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