Mutex object, event object, key code segment

Source: Internet
Author: User

I recently reviewed multithreading knowledge.

Similar roles and different focuses

Critical Zone: Shared Resources
Synchronization between mutually exclusive object threads
Event object: It is generally used in complicated areas and can transmit some information.

The following is a comparison between mutex objects, event objects, and key code segments:

1. mutex object

# Include <iostream> # include <windows. h> using namespace STD; // declare two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter // thread data); DWORD winapi threadproc2 (lpvoid lpparameter // thread data ); // number of global votes int gticket = 100; // mutually exclusive object handle hmutex; int main () {// create two thread handles handle hthread1 = createthread (null, 0, threadproc1, null, 0, null); handle hthread2 = createthread (null, 0, threadproc2, null, 0, null); // create a mutex object hmutex = createmutex (null, false, null ); // close two thread handles closehandle (hthread1); closehandle (hthread2); // The main thread sleeps for 4 seconds, so that the two thread functions can obtain the CPU time slice sleep (4000 ); system ("pause"); Return 0;} // defines two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter) {While (true) {waitforsingleobject (hmutex, infinite ); if (gticket> 0) {cout <"thread1 javasticket" <gticket -- <Endl;} else {break;} releasemutex (hmutex);} return 0 ;} DWORD winapi threadproc2 (lpvoid lpparameter) {While (true) {waitforsingleobject (hmutex, infinite); If (gticket> 0) {cout <"thread2 javasticket" <gticket -- <Endl;} else {break;} releasemutex (hmutex);} return 0 ;}


2 event object

# Include <windows. h ># include <iostream> using namespace STD; // declare two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter // thread data ); DWORD winapi threadproc2 (lpvoid lpparameter // thread data); // global variable int gticket = 100; // event object handle hevent; int main () {// create two thread handles: handle hthread1 = createthread (null, 0, threadproc1, null, 0, null); handle hthread2 = createthread (null, 0, threadproc2, null, 0, null); // creation time object hevent = createevent (null, false, true, null); // close two thread handles closehandle (hthread1); closehandle (hthread2 ); // The main thread sleeps for 4 seconds, giving other thread functions the opportunity to get the CPU time slice sleep (4000); System ("pause"); Return 0 ;} // define two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter) {While (true) {waitforsingleobject (hevent, infinite); If (gticket> 0) {cout <"thread1 effecticket" <gticket -- <Endl;} else {break;} setevent (hevent);} return 0;} DWORD winapi threadproc2 (lpvoid lpparameter) {While (true) {waitforsingleobject (hevent, infinite); If (gticket> 0) {cout <"thread2 implements ticket" <gticket -- <Endl ;} else {break;} setevent (hevent);} return 0 ;}

3 key code segments

# Include <iostream> # include <windows. h> using namespace STD; // declare two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter // thread data); DWORD winapi threadproc2 (lpvoid lpparameter // thread data ); // number of global votes int gticket = 100; // key code segment critical_section gsection; int main () {// initialize key code segment, which must be created before the thread initializecriticalsection (& gsection ); // create two thread handles: handle hthread1 = createthread (null, 0, threadproc1, null, 0, null); handle hthread2 = createthread (null, 0, threadproc2, null, 0, null); // close two thread handles closehandle (hthread1); closehandle (hthread2); // The main thread sleep for 4 seconds sleep (4000 ); // Delete the key code segment deletecriticalsection (& gsection); System ("pause"); Return 0 ;}// define two thread functions: DWORD winapi threadproc1 (lpvoid lpparameter) {While (true) {entercriticalsection (& gsection); // enter the key code segment if (gticket> 0) {cout <"thread1 javasticket" <gticket -- <Endl ;} else {break;} leavecriticalsection (& gsection); // exit key code segment} return 0;} DWORD winapi threadproc2 (lpvoid lpparameter) {While (true) {entercriticalsection (& gsection ); // enter the key code segment if (gticket> 0) {cout <"thread2 javasticket" <gticket -- <Endl ;}else {break;} leavecriticalsection (& gsection ); // exit key code segment} return 0 ;}

The program running result is clear to everyone. However, the result of the first two kernel objects (mutex object and event object) is somewhatUnexpected.

Take the event object as an example. When I used breakpoint debugging, I found this problem. The function of thread 1 obtains the ownership of the mutex object (the ownership is not released), switches the CPU time slice, enters the waitforsingleobject of thread 2 function, and then calls the cout of thread 2 function <"thread2
Sell ticket "<gticket -- <Endl;, what's the matter? The thread 1 function has not been released yet. How can the thread 2 function still run?

When mutex objects are used, the displayed results sometimes look like this. Think twice about it.


I hope you can give us a clear picture. Give some comments.


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.