passing parameter to thread

A

asit

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);
}
}
 
C

CBFalconer

asit said:
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).
 
J

jacob navia

asit said:
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?
 
A

asit

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.
 
J

jacob navia

asit said:
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
 
A

asit

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

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

long live comp.lang.c community members
 
A

Antoninus Twink

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".
 
K

Kenny McCormack

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top