Windows thread Synchronization

Source: Internet
Author: User
Tags semaphore

When it comes to Windows thread synchronization, there are cyclic locks, critical sections (critical code Snippets), and kernel objects (EVENT,SEMAPHORE,MUTEX). Even processes, thread handle, and so on.

To talk about usage:

Both critical sections and mutexes can be used to control mutually exclusive access to shared resources, where the critical section is a user-level object, consumes little and is fast, but cannot cross processes. Mutexes are kernel objects that are slow, but can span processes.

Semaphore can be used for resource control, two key parameters of the semaphore, the number of existing resources and the maximum number of resources, can be used to control the resources, through releasesemaphore to increase the resources, waitfor* function to reduce resources.

The event can be used to notify that the status of the event can be changed manually to inform all waiting threads (manual reset event) or whether a single thread (auto reset event) is running.

Tell me more about the note:

The mutex is a thread-path attribute of the mutex, and if id=0 is the notification state, the waitfor* function returns, the ID is set to the fetched Id,releasemutex after the ID is set to 0, and the other thread has the opportunity to obtain the mutex. If the thread does not exit with an ReleaseMutex exception, the mutex is also freed, the ID is set to 0, and the other threads have the opportunity to obtain the mutex.

There are two uses of event, one is auto-reset and one is manual reset. An automatic reset resets to no notification after waitfor* acquires the event, so that only one waiting thread (waitfor*) returns. A manual reset will wait until resetevent is reset to the non-notification state, so all waiting threads (waitfor*) will return.

Semaphore can control the number of concurrently running threads, the maximum number of resources determines the number of threads running at the same time, and the number of existing resources determines the number of threads that can now run. (waitfor*) To reduce resources, releasesemaphore increase the number of resources, the total number of resources does not exceed the maximum number of resources.

Windows thread Synchronization

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.