Reputation: 93
I am a beginner in Multithreading(Windows). I want to create 2 threads by using CreateThread function in a for loop. But two threads functions which I want to spawn are different. So I am creating an array of function pointers to store the two functions which I want to spawn. I will use the array values in the lpStartAddress parameter of create thread function. But I am getting error in creating array of function pointers. I have post only the significant part of the code below. So please correct my mistake and help me to create an array of the function pointers (Whose functions are going to be spawned as threads). Thanks in advance.
DWORD WINAPI Threadproc1(LPVOID lparam)
{
print_func(GetCurrentThreadId(),(LPDWORD)lparam);
return 1;
}
DWORD WINAPI Threadproc2(LPVOID lparam)
{
print_func(GetCurrentThreadId(),(LPDWORD)lparam);
return 1;
}
int main()
{
HANDLE hThread[MAX_THREADS] = {NULL};//MAX_THREADS=2
DWORD dwthreadid;
/* Array of lparam */
DWORD dwArrayparam[PARAM_MAX] = {1,2};//PARAM_MAX=2
/* Array of function ptrs */
typedef DWORD WINAPI (*t_Threadproc)(LPVOID);//ERROR in this line
t_Threadproc Threadproc[MAX_THREADS] = {Threadproc1,Threadproc2};
for(int i=0; i<MAX_THREADS; i++)
{
hThread[i] = CreateThread(NULL,//security attributes
0,//stack size
Threadproc[i],//thread start address
(dwArrayparam+i),
0,
&dwthreadid
);
}
}
Compilation error: error that iam getting is error C2059: syntax error : '(' if I remove the calling convention WINAPI iam getting error in the next line error C2440: 'initializing' : cannot convert from 'DWORD (__stdcall *)(LPVOID)' to 't_Threadproc'
Upvotes: 0
Views: 241
Reputation: 2349
I believe the line should look like this:
typedef DWORD (WINAPI *t_Threadproc)(LPVOID);
i.e. the keyword WINAPI
needs to be inside the parenthesis.
Upvotes: 4