The critical section can be used to achieve thread synchronization. The event and mutex (mutex) can also be done.
Thread events in the Win32 API
HANDLE hevent = NULL;
void maintestfun{
hevent = CreateEvent (null,false,false,null);
SetEvent (hevent);
Char g_chararray[4];
CString Szresult;
Any of the following three threads will not be accessible to other threads when accessing G_chararray
AfxBeginThread (Funone,null); Funone array assignment is all s
AfxBeginThread (Funtwo,null); Funtwo also assigns the array a value of all B
AfxBeginThread (GetResult,); Back to BBBB
}
The other two functions Funone or getresult also add two lines of such code. The definition of these two functions is shown in the previous article http://blog.csdn.net/weiwenhp/article/details/8650896
UINT Funone (LPVOID pparam) {
WaitForSingleObject (hevent, INFINITE); like EnterCriticalSection, the infinite here says it will wait until the other threads have finished executing. If written in 1000, it means only 1 seconds. 1 seconds
The thread is automatically aborted if the other thread has not freed the resource yet.
for (int i =0; i < 4; i++) {
G_chararray[i] = ' S ';
Sleep (1);
}
SetEvent (hevent); similar LeaveCriticalSection
return 0;
}
Thread event classes in MFC
CEvent g_cevent;// Instantiation of an event class
void maintestfun{
G_cevent.setevent ();
Char g_chararray[4];
CString Szresult;
Any of the following three threads will not be accessible to other threads when accessing G_chararray
AfxBeginThread (Funone,null); Funone array assignment is all s
AfxBeginThread (Funtwo,null); Funtwo also assigns the array a value of all B
AfxBeginThread (GetResult,); Back to BBBB
}
The other two functions Funone or getresult also add two lines of such code. The definition of these two functions is shown in the previous article http://blog.csdn.net/weiwenhp/article/details/8650896
UINT Funone (LPVOID pparam) {
WaitForSingleObject (g_cevent, INFINITE);// similar EnterCriticalSection
The thread is automatically aborted if the other thread has not freed the resource yet.
for (int i =0; i < 4; i++) {
G_chararray[i] = ' S ';
Sleep (1);
}
g_cevent. SetEvent (); similar LeaveCriticalSection
return 0;
}
Mutex (mutex) in the Win32 API
HANDLE Hmutex = NULL;
void maintestfun{
Hmutex= CreateMutex (null,false,null);
Char g_chararray[4];
CString Szresult;
Any of the following three threads will not be accessible to other threads when accessing G_chararray
AfxBeginThread (Funone,null); Funone array assignment is all s
AfxBeginThread (Funtwo,null); Funtwo also assigns the array a value of all B
AfxBeginThread (GetResult,); Back to BBBB
}
The other two functions Funone or getresult also add two lines of such code. The definition of these two functions is shown in the previous article http://blog.csdn.net/weiwenhp/article/details/8650896
UINT Funone (LPVOID pparam) {
WaitForSingleObject (hmutex, INFINITE);// similar to EnterCriticalSection, The infinite here says it will wait until the other threads have finished executing. If written in 1000, it means only 1 seconds. 1 seconds
The thread is automatically aborted if the other thread has not freed the resource yet.
for (int i =0; i < 4; i++) {
G_chararray[i] = ' S ';
Sleep (1);
}
ReleaseMutex (hmutex );//similar to LeaveCriticalSection
return 0;
}
Mutex classes in MFC (CMutex)
CMutex G_cmutex;
void maintestfun{
Char g_chararray[4];
CString Szresult;
Any of the following three threads will not be accessible to other threads when accessing G_chararray
AfxBeginThread (Funone,null); Funone array assignment is all s
AfxBeginThread (Funtwo,null); Funtwo also assigns the array a value of all B
AfxBeginThread (GetResult,); Back to BBBB
}
The other two functions Funone or getresult also add two lines of such code. The definition of these two functions is shown in the previous article http://blog.csdn.net/weiwenhp/article/details/8650896
UINT Funone (LPVOID pparam) {
G_cmutex.lock ();//similar to EnterCriticalSection, the infinite here says it will wait until the other threads have finished executing. If written in 1000, it means only 1 seconds. 1 seconds
The thread is automatically aborted if the other thread has not freed the resource yet.
for (int i =0; i < 4; i++) {
G_chararray[i] = ' S ';
Sleep (1);
}
G_cmutex.unlock ();// similar to LeaveCriticalSection
return 0;
}
MFC Threads (iii): Thread synchronization events (event) and mutex (mutex)