Multi-threading.

Hi,

I am doing a multi-threading program, in that i got some errors and i could not able to resolve them please help me.

This is my code.

#include"stdafx.h"
#include<iostream>
#include <process.h>
#include <Windows.h>
#include"thread_headerfile.h"

using namespace std;

void isPrime()
{
cout<<"prime";

}



unsigned int __stdcall mythreadA()
{

isPrime();

return 0;
}


int main(int argc, char* argv[])
{
HANDLE myhandleA,myhandleB,myhandleC;

myhandleA=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
myhandleB=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
myhandleC=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
WaitForSingleObject(myhandleA, INFINITE);
WaitForSingleObject(myhandleB, INFINITE);
WaitForSingleObject(myhandleC, INFINITE);
CloseHandle(myhandleA);
CloseHandle(myhandleB);
CloseHandle(myhandleC);

getchar();

return 0;
}

and the error I am getting is

error C2059: syntax error : ',' at the three lines where the thread call is invoking.

Please help me.
Without knowing anything, this looks suspicious: (void*), 0
Please use code brackets:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include"stdafx.h"
#include<iostream>
#include <process.h>
#include <Windows.h>
#include"thread_headerfile.h"

using namespace std;

void isPrime() {
cout<<"prime";

}



unsigned int __stdcall mythreadA()  { 

isPrime();

return 0;
}


int main(int argc, char* argv[]) {
HANDLE myhandleA,myhandleB,myhandleC;

myhandleA=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
myhandleB=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
myhandleC=(HANDLE)_beginthreadex(0, 0, &mythreadA,(void*), 0, 0);
WaitForSingleObject(myhandleA, INFINITE);
WaitForSingleObject(myhandleB, INFINITE);
WaitForSingleObject(myhandleC, INFINITE);
CloseHandle(myhandleA);
CloseHandle(myhandleB);
CloseHandle(myhandleC);

getchar();

return 0;
}


What is the header file?
If I am replacing (void*) with a 0 then I am getting this error-

error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall *)(void)' to 'unsigned int (__stdcall *)(void *)'
I must guess that the declaration is:
1
2
3
4
5
6
7
8
uintptr_t _beginthreadex( // NATIVE CODE
   void *security,
   unsigned stack_size,
   unsigned ( __stdcall *start_address )( void * ),
   void *arglist,
   unsigned initflag,
   unsigned *thrdaddr 
);

When you had (void*) as the fourth parameter, you had a syntax error.
C++ syntax of this cast is: static_cast<void*>(), which is more clearly an error, because there is nothing to cast.

The proper syntax is: static_cast<void*>(0) or (void*)0. Plain 0 converts implicitly though.


The new error:
The function expects unsigned ( __stdcall * )( void * )
Your mythreadA is a unsigned ( __stdcall *)()
Close, but yet so far.
Topic archived. No new replies allowed.