MFC Threads (iii): Thread synchronization events (event) and mutex (mutex)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.