How to Create a Multithread for “public ref class” in VS c++ with “_beginthread”

How to Create a Multithread for “public ref class” in Windows c++ (VS) with “_beginthread” instead of using gcnew Thread


Intention: To synchronize common variables (update & retrieve data)in 3 different threads accessing 3 different public reference class.

Sample code:

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
42
43
44
45

int xppp;
public ref class A  
{  
	public:  
	void fun(void *)  
	{	while(xppp<99)
		{  
			WaitForSingleObject(hPrintMutex,INFINITE);  
			xppp+=2;  
			cout<<xppp<<endl;
		ReleaseMutex(hPrintMutex);  
			Sleep(10);
		}  		
	}
};

//Similarly 
public ref class B{};
public ref class C{};	

int main()
{
	x=0;
	hPrintMutex=CreateMutex(NULL,false,NULL);	
	B M2;
	HANDLE hThread1=(HANDLE)_beginthread(B::fun,0,&M2);
	A M1;
	HANDLE hThread2=(HANDLE)_beginthread(A::fun,0,&M1);
	C M3;
	HANDLE hThread3=(HANDLE)_beginthread(C::fun,0,&M3);
	WaitForSingleObject(hThread1,INFINITE);
	WaitForSingleObject(hThread2,INFINITE);
	WaitForSingleObject(hThread3,INFINITE);
  
    return 0;  
}

/* I also tried...
But unable to use the "WaitForSingleObject" which will synchronize  

A^ M1=gcnew A();  
Thread^ MainThread1 = gcnew Thread(gcnew ThreadStart(M1,&A::fun));  
MainThread1->Start();
MainThread1->Join(); */
Which wait function should i use to synchronize a variable if i use Thread library as below to create thread for a public reference class.

public ref class A{};

main()
{

A^ M1=gcnew A();
Thread^ MainThread1 = gcnew Thread(gcnew ThreadStart(M1,&A::fun));
MainThread1->Start();
MainThread1->Join();
}
I found a interesting reference in this website:

people who are having same problem can refer to the link below:
http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018
Topic archived. No new replies allowed.