By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,012 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,012 IT Pros & Developers. It's quick & easy.

passing parameter to thread

P: n/a
How do I pass parameters to created thread..here is the code(but it
can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

DWORD Sum;

DWORD WINAPI Summation(LPVOID Param)
{
DWORD Upper = *(DWORD*)Param;
for(DWORD i=0; i<= Upper; i++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])
{
DWORD ThreadId;
HANDLE ThreadHandle;
int Param;

if(argc != 2)
{
fprintf(stderr, "usage : %s number", argv[0]);
return 1;
}

Param = atoi(argv[1]);
if( Param < 0)
{
fprintf(stderr, "an integer >= 0 is required\n");
return 1;
}

ThreadHandle = CreateThread(
NULL, //default security attributes
0, //default stack size
Summation, //thread function
&Param, //parameter to thread function
0, //default creation flags
&ThreadId); //returns the thread identifier

if (ThreadHandle != NULL)
{
//wait for thread to finish
WaitForSingleObject(ThreadHandle, INFINITE);
//close the thread handle
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
}
Oct 6 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
asit wrote:
>
How do I pass parameters to created thread..here is the code (but
it can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>
Threads and windows are off-topic on c.l.c, which deals with the C
language as defined in the ISO C standards. C does not contain
threads, nor windows. Look for a group that deals with your system
(probably Windows).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 6 '08 #2

P: n/a
asit wrote:
How do I pass parameters to created thread..here is the code(but it
can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

DWORD Sum;

DWORD WINAPI Summation(LPVOID Param)
{
DWORD Upper = *(DWORD*)Param;
for(DWORD i=0; i<= Upper; i++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])
{
DWORD ThreadId;
HANDLE ThreadHandle;
int Param;

if(argc != 2)
{
fprintf(stderr, "usage : %s number", argv[0]);
return 1;
}

Param = atoi(argv[1]);
if( Param < 0)
{
fprintf(stderr, "an integer >= 0 is required\n");
return 1;
}

ThreadHandle = CreateThread(
NULL, //default security attributes
0, //default stack size
Summation, //thread function
&Param, //parameter to thread function
0, //default creation flags
&ThreadId); //returns the thread identifier

if (ThreadHandle != NULL)
{
//wait for thread to finish
WaitForSingleObject(ThreadHandle, INFINITE);
//close the thread handle
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
}
I can't understand your problem. You are passing the "Param" parameter
to your thread. What is actually your problem?
Which parameters you can't pass?
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 6 '08 #3

P: n/a
sorry.....I didn't metion.

My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.
Oct 6 '08 #4

P: n/a
asit wrote:
sorry.....I didn't metion.

My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.
To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 6 '08 #5

P: n/a
On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
asit wrote:
sorry.....I didn't metion.
My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.

To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatiquehttp://www.cs.virginia.edu/~lcc-win32
Thank you....life is so easy because of this community !!!!

long live comp.lang.c community members
Oct 6 '08 #6

P: n/a
On 6 Oct 2008 at 8:38, asit wrote:
On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
[good advice]
Thank you....life is so easy because of this community !!!!
Because of a depressingly small subset of this community, unfortunately.

The sad truth is that the unhelpful response you got from CBF is
representative of the vast majority of the clc posting "community".

Oct 6 '08 #7

P: n/a
In article <ac**********************************@b38g2000prf. googlegroups.com>,
asit <li*****@gmail.comwrote:
>On Oct 6, 1:18 pm, jacob navia <ja...@nospam.comwrote:
>asit wrote:
sorry.....I didn't metion.
My objective is to pass a no of parameters..and also my child thread
needs to call some function of main thread.

To pass several integers you could just pass the address of an
array. In the first position you would put the number of
integers in the array.

If you want to call something in the main thread you just call it.
All the functions of the main thread are available to you without any
restriction

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatiquehttp://www.cs.virginia.edu/~lcc-win32

Thank you....life is so easy because of this community !!!!

long live comp.lang.c community members
Well, some of them, anyway...

Oct 6 '08 #8

P: n/a
In article <48**************@yahoo.com>,
senile old fool CBFalconer <cb********@maineline.netwrote:
>asit wrote:
>>
How do I pass parameters to created thread..here is the code (but
it can't pass data..it uses global variable)

#include <windows.h>
#include <stdio.h>

Threads and windows are off-topic on c.l.c, which deals with the C
language as defined in the ISO C standards. C does not contain
threads, nor windows. Look for a group that deals with your system
(probably Windows).
Way to go, Chuckie!

Oct 6 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.