Process Synchronization in Windows

Source: Internet
Author: User

Today, I opened mfcwindows program design and patiently read this chapter. So I wrote something for later use.

 

Multi-thread synchronization:

1. Critical Section

Only the threads of the same process can be synchronized, because the critical section cannot work across the process boundary. Because the critical section does not have a name, it cannot be used across processes.

Before accessing the critical sectionLock.Unlock.

If process B accesses the critical section locked by process a, process B is blocked until thread a releases the critical section and thread B can run. No CPU time is occupied during thread B's blocking.

2. mutex

It can be synchronized between the same process or threads of different processes. Therefore, the mutex has a name and can be used across processes.

The difference between mutex and critical section. If a thread locks the critical section but does not unlock the lock at the end, other threads waiting for the idle section will be blocked indefinitely. However, if the thread that locks the mutex cannot unlock the mutex before its termination, the system considers that the mutex is "abandoned" and automatically releases the mutex, in this way, the process can continue to run.

3. Events

At any specific time, an event can only be in one of two States: triggering (setting) or lowering (resetting ). The task can be set out of the signal state, and the reset event can be considered out of the non-signal state.

The event is also described as a "thread trigger ". Events cannot be cross-process. It also has no name.

Events can be set automatically or manually. The difference is that when the blocked thread on the automatic setting event is awakened, the event is not automatically set to the signal state. Manual setting events cannot be reset automatically. They must be reset programmatically.

L if an event only triggers one thread, use the automatic setting event and setevent to wake up the waiting thread.

Resetevent does not need to be called here, because the event is automatically reset when the thread is awakened.

L if the event will trigger two or more threads, manually set the thread and use pulseevent to wake up the waiting thread. Additionally, you do not need to call resetevent because pluseevent resets the event for you after waking up all the waiting threads.

4. semaphores

First, semaphores can synchronize different processes with the same threads.

Second, unlike the other three types of synchronization mechanisms, semaphores have different features such as "none or none" and semaphores, it always indicates the number of available resources. Locking semaphores reduces the number of resources, and releasing semaphores increases the number of resources. The thread is blocked only when the number of resources is 0.

Reference: http://blog.csdn.net/renjwjx/article/details/3917407

Today, I opened mfcwindows program design and patiently read this chapter. So I wrote something for later use.

 

Multi-thread synchronization:

1. Critical Section

Only the threads of the same process can be synchronized, because the critical section cannot work across the process boundary. Because the critical section does not have a name, it cannot be used across processes.

Before accessing the critical sectionLock.Unlock.

If process B accesses the critical section locked by process a, process B is blocked until thread a releases the critical section and thread B can run. No CPU time is occupied during thread B's blocking.

2. mutex

It can be synchronized between the same process or threads of different processes. Therefore, the mutex has a name and can be used across processes.

The difference between mutex and critical section. If a thread locks the critical section but does not unlock the lock at the end, other threads waiting for the idle section will be blocked indefinitely. However, if the thread that locks the mutex cannot unlock the mutex before its termination, the system considers that the mutex is "abandoned" and automatically releases the mutex, in this way, the process can continue to run.

3. Events

At any specific time, an event can only be in one of two States: triggering (setting) or lowering (resetting ). The task can be set out of the signal state, and the reset event can be considered out of the non-signal state.

The event is also described as a "thread trigger ". Events cannot be cross-process. It also has no name.

Events can be set automatically or manually. The difference is that when the blocked thread on the automatic setting event is awakened, the event is not automatically set to the signal state. Manual setting events cannot be reset automatically. They must be reset programmatically.

L if an event only triggers one thread, use the automatic setting event and setevent to wake up the waiting thread.

Resetevent does not need to be called here, because the event is automatically reset when the thread is awakened.

L if the event will trigger two or more threads, manually set the thread and use pulseevent to wake up the waiting thread. Additionally, you do not need to call resetevent because pluseevent resets the event for you after waking up all the waiting threads.

4. semaphores

First, semaphores can synchronize different processes with the same threads.

Second, unlike the other three types of synchronization mechanisms, semaphores have different features such as "none or none" and semaphores, it always indicates the number of available resources. Locking semaphores reduces the number of resources, and releasing semaphores increases the number of resources. The thread is blocked only when the number of resources is 0.

Reference: http://blog.csdn.net/renjwjx/article/details/3917407

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.