Summary of Windows Thread Synchronization Mechanism

Source: Internet
Author: User

Some core objects such as thread, process, evnet, mutex, and semaphore are used for synchronization between threads.
Use wait functions such as waitforsingleobjects and waitformultipleobjects between threads.
Wait for the function to use the handle of the core object as the parameter. If handle is fired, execute the next step.
Conditions when handle is triggered: (handle is a memory pointer, a type conversion pointer made to hide internal implementations)
Inspired: --- I understand that resources are not defeated.
Not stimulated: --- the resource is being occupied.
Eg:
1) If thread and process are terminated, the thread is triggered.
2) Event: it is the most flexible method to be manually triggered through its API and can be used by all threads.
3) mutex: triggered if it is not owned by any thread.

1) critical section: critical_section
Scope of application: Each thread of a single process is used to occupy it.
Feature: local object; fast and effective. Unable to monitor whether it is abandoned by the thread
Function: entercriticalsection leavecriticalsection

2) mutex:
Applicability: Different threads are used to rank and occupy each other.
Feature: core object, which can be waited by wait and can only be released by the owner thread
Function: createmutex releasemutex

3) semaphore: semaphore
Applicability: used to limit resource usage
Feature: core object, with no owner, can be released by any thread
Function: createsemaphore opensemaphore releasesemaphore

4) event:
Applicability: it is used to control the reception of object signals and is often used together with the signal system.
Features: core objects
Function: createevent openevent pulseevent setevent resetevent
 
5) interlocked
Simple atomic operations, such as locking the byte range in a file in a Write File

Note: the most important thing in thread synchronization can be summarized as lock system lock and signal system signal.
Lock includes: critical_section, mutex,
Wait function:
Waitformultipleobjects waitforsingleobject sleep
 
6) completion port
Applicability: Asynchronous Network reception, including file read/write
Feature: read/write is controlled by the OS. It is the most effective synchronization mechanism on the Windows platform, which is equivalent to AIO or non-blocking Socket + epoll in Linux.
Function: createiocompletionport getqueuedcompletionstatus

Example 1: Event
// Event mechanism: sets a global event object, which can only wait for a maximum of 64 objects, and uses waitformultipleobjects to monitor the line

The Process Handle array is not as good as the full port completion port
Handle ghwriteevent;
Handle ghthreads [threadcount];
// (1) create a manual event and do not accept any signal No signel at the beginning
// Resetevent: used for signal reset, same as createevent or openevent
Ghwriteevent = createevent (
Null, // default security attributes
True, // Manual-Reset event
False, // initial state is nonsignaled
Text ("writeevent") // Object Name
);
// (2) generate a bunch of threads and set the Event Response signal,
If (! Setevent (ghwriteevent ))
{
Printf ("setevent failed (% d)/n", getlasterror ());
Return;
}
// (3) set the thread wait event. All threads receive this event. The thread is synchronized here. The next step is executed only when all threads are executed.
Dwwaitresult = waitformultipleobjects (
Threadcount, // Number of handles in array
Ghthreads, // array of thread handles
True, // wait until all are signaled
Infinite );
{
// (3.1) Each thread function is waiting for the event object, and the thread is also synchronized here. Only the thread that obtains the signal can execute the next step.
Dwwaitresult = waitforsingleobject (
Ghwriteevent, // event handle
Infinite); // indefinite wait

}
// (4) disable this global event = closehandle (ghwriteevent)
Closeevents ();

Example 2: completion port
(1) create a complete port and associate it with a listening socket. createiocompletionport
(2) generate a bunch of threads so that the threads can wait for. createthread in full port loop -- (workerthread )--

Getqueuedcompletionstatus
(3) receive the read/write request accept from the listening socket and associate the accept socket with the full port ,....?

References:
The http://msdn2.microsoft.com/en-us/library/ms686360 (vs.85). aspx

Related Article

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.