Synchronization between three threads

Source: Internet
Author: User

First mutex object
Createmutex (,,)
The first parameter is the security level, and null is the default value.
The second parameter is the owner, and false indicates that there is no owner.
The third parameter is a mutex object, and null is anonymous (if not anonymous, "Naming object mutex" means only one instance object can be created)
Returns a handle.

Mutex is related to the thread. There is a thread ID and a count ID internally to check the number of times referenced. The number of times releasemutex () must be called after multiple references. If the current thread has

Terminate, the mutex is automatically released, and the count is reduced by one.

Request mutex object:
Waitforsingleobject (,) // whether it is in a signal state
The first handle Parameter
Interval of the second parameter
Returned value: If the thread is terminated abnormally or no releasemutex exists before the thread is terminated, wait_abandoned is returned.

Note: handle can be the handle of the following objects:

Change Notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable Timer

Closehandle () is required after creation ()

Releasemutex ()

Second, event object

The event object is also a kernel object and can be synchronized between threads.
Createevent (,,,)
The first parameter, security level. null is the default level.
The second parameter, manual or automatic reset, true, requires manual resetevent (), flase, the system automatically reset to non-Signal Status
The third parameter specifies the event initialization status, which is true and has the Signal status, flase, and non-Signal status. (You can also call setevent () after creation to set whether the event is in the signal status)
Fourth parameter, the name of the event object

Waitforsingleobject () Here event is used as the parameter

Third keyCodeSection (critical section)

Initializecriticalsection ()

Entercriticalsection (); // determines whether the code can be obtained. If the code segment can be accessed
Leavecriticalsection (); // release the ownership after the access is completed.

Deletecriticalsection ();

Comparison between the three

The mutex object and event object belong to the kernel object. The thread synchronization using the kernel object is slow, but the kernel object such as the mutex object and event object can be used in multiple processes.

.
Key code segments (critical sections) work in the user mode, and the synchronization speed is fast. However, when using key code segments, it is easy to enter the deadlock status, because it is not possible to wait for key code segments to enter.

Set the timeout value.

 

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.