Synchronization control for Threads (synchronization)

Source: Internet
Author: User
Tags mutex

  • critical section (Critical Sections)
    • Summary

      • The Critical Zone (Critical section) is used to achieve "exclusive possession". Fits between the threads of a single process at the scope.
    • Characteristics
      • A local object, not a core object
      • Fast and efficient
      • Not be able to have at the same time more than one critical section is waiting
      • Unable to detect whether a thread has been discarded
    • Related functions
      • Definition: critical_section
      • Initialization: Initializacriticalsection ()
      • Access: EnterCriticalSection ()
      • Departure: LeaveCriticalSection ()
      • Delete: DeleteCriticalSection ()
    • Deadlock (deadlock)
      • Any time a piece of code requires two (or more) resources, there is a potentially deadlock shadow. The question of dining as philosophers.
  • Mutex (mutexes)
    • Summary
      • A Mutex is a core object that can achieve "exclusive possession" between different threads, even if those threads belong to different processes.
    • Characteristics
      • A Core object
      • If the thread that owns the mutex ends, a "abandoned" error message is generated
      • You can use wait ... () waits for one or more mutexes
      • Can be named so that it can be opened by another process
      • Can only be freed by the thread that owns it.
    • related functions
      • open: OpenMutex ()
      • wait (or enter): Waitforsigleobject (), WaitForMultipleObjects (), MsgWaitForMultipleObjects ()
      • release: ReleaseMutex ()
      • Span class= "Content mubu-node" > delete: CloseHandle ()
    • Mutex and Critical section comparison
      • Locking a mutex that is not owned can take almost 100 times times more time than locking an critical section that is not owned. Because the critical section runs on UserMode.
      • Mutexes can be used across processes. Critical section can only be used in the same process
      • When you wait for a mutex, you can specify the length of time to end waiting. Critical section is not.
      • A mutex can be set to wait for multiple simultaneous critical sections to wait for only one
  • signal Volume (semaphores)
      • summary
        • semaphores is used to track limited resources
      • features
        • is a core object
        • No owner
        • can be named so that it can be opened by another process
      • related functions
        • get: WaitForSingleObject (), WaitForMultipleObjects ()
        • release: ReleaseSemaphore ()
  • Events (Event)
    • Summary
      • The Event object is typically used for overlapped I/O, or for designing some custom synchronization objects
    • Characteristics
      • is a core object
      • Completely under the control of the program
      • Suitable for designing new synchronization objects
      • The request to wake up is not stored and may be forgotten
      • Can be named so that it can be opened by another process
    • related functions
      • reset: ResetEvent ()
    • Comparison of excitation functions
      • Automatic mode (auto Reset Event):
        • PulseEvent and SetEvent function the same, after the call is waiting for the event, the suspended individual thread will enter the active state, and the event is then set back to no signal.
      • Manual mode (Manual Reset Event):
        • PulseEvent: After a call is waiting for an event, a single thread that is suspended will enter the active state, and the event is then set back to no signal.
        • SetEvent: After the call is waiting for the event, all suspended threads go into the active state and wait for the resetevent call to set the event back to no signal.
  • Interlocked Variable
    • Summary
      • If interlocked ... () functions are used in so-called Spin-lock, then they are just a synchronization mechanism. The so-called Spin-lock is a busy loop that is expected to execute in a very short period of time, so there is a minimum additional burden (overhead). The system cores occasionally use them. In addition to this only, interlocked variable is primarily used for reference counting.
    • Characteristics
      • There are some basic synchronization operations on 4-byte values, no need to use critical section or Mutex, etc.
      • Efficient operation in SMP (symmetric multi-processors) operating system
    • Related functions
      • Add one: InterlockedIncrement ()
      • Minus One: InterlockedDecrement ()
      • Set Value: InterlockedExchange




Synchronization control for Threads (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.