Synchronization mechanisms for Windows operating systems

Source: Internet
Author: User

Recently looked at the Windows core programming, although is the older book, but the feeling is quite useful, incidentally reviewed some operating system knowledge, such as the process synchronization mechanism of this article

Thread synchronization in user mode

Communication is required between threads:

1. Multiple threads need to access one shared resource at the same time without destroying the integrity of the resource

2. One thread needs to notify other threads that a task has been completed

Method One: Atomic access: Interlocked series functions

InterlockedExchangeAdd (long & int);

InterlockedIncrement;

InterlockedExchange (it is useful to implement a rotary lock)

The variable addresses passed to these functions must be guaranteed to be aligned or the function may fail

The INTERLOCKDE function does not need to switch between kernel mode and user mode

Rotate Lock: Disable thread priority elevation

Multi-processor machine rotation Lock is more useful,

Key segment: Cycle a certain number of times, if still inaccessible, the thread switches to kernel mode until the resource is available

InterlockedCompareExchange

Method Two: Key segment

Critical_section G_cs;

EnterCriticalSection (&g_cs)

...

Leavecrititalsection (&g_cs)

Critical segments cannot synchronize threads between multiple processes

Method Three: Slim read/write lock

Similar to critical segments, but distinguish between read and write threads

Srwlock

Thread synchronization with kernel objects

There is a Boolean variable inside the process kernel object that initializes the value of this variable to false (not triggered) when the system creates the kernel object, and when the process terminates, the operating system sets the value in the corresponding kernel object to True, indicating that it has been triggered.

Wait function: WaitForSingleObject (Handle,dword)

WaitForMultipleObjects (Dword,const handle*, Bool,dword)

Event Kernel Object

Let one thread perform the initialization work, then start another thread and let it perform the rest of the work

CreateEvent ()

OpenEvent ()

SetEvent ()//when triggered

RESETEVETN ()

The counter kernel object that can wait

CreateWaitableTimer ()

Openwaitabletimer ()

SetWaitableTimer ()//when triggered

Signal Volume

Mutex Amount

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.