Five kinds of processes or threads synchronized exclusive control method

Five kinds of processes or threads synchronized exclusive control method

  • The critical area: the serial multi-threaded access to public resources or through a piece of code, speed control for data access.
  • Mutex / mutex: to coordinate common for individual access to a shared resource designed; because the kernel mode, the performance is worse than critical region; across processes.
  • Spin locks: one kind of mutex implementation, while waiting for the CPU will take, whether to be released by the judge lock loop, so more quickly, but has CPU time.
  • Semaphore: control with a limited number of users and resources designed mutex is to be understood that a user of the resource semaphore.
  • Event: used to notify the thread has some event has occurred, which initiates the beginning of the subsequent tasks.

First, the mutex (mutex)

    In a multitasking operating system to run multiple tasks at the same time you may need to use the same resources. For example, the same file, a thread may be a write operation, while another thread need to read this document, we can imagine, if the writer thread is not over written, but this time the reader thread began, or read the thread has not read and write the end of the thread starts, then the end result will obviously be confusing. In order to protect shared resources in the thread have such a lock - mutex (mutex), mutex lock is an easy way to control access to shared resources, mutex only two states, That lock (lock) and unlock (unlock).

    C ++ Multithreading: mutex https://blog.csdn.net/qq_28114615/article/details/88367016
    c ++ thread several locks: https://blog.csdn.net/bian_qing_quan11/article/details/73734157

Second, the critical area (Critical Section)

    Easy way to ensure that only one thread can access the data at a time. At any time only one thread access to shared resources. If there are multiple threads attempt to simultaneously access a critical section, then all other attempts to access this critical area of ​​the thread will be suspended in one thread entered, and continued until the thread enters the critical zone to leave. After the critical section is released, the other threads can continue to seize, and thus achieve the purpose of operating a shared resource atomically.

Third, the semaphore (Semaphores)

    Semaphore objects synchronized manner with the thread in front of several different methods, signals allows multiple threads to use a shared resource, which is same as the operation of the PV system operation. It noted that the maximum number of simultaneous threads access to shared resources. The maximum number of threads that allows multiple threads access the same resource at the same time, but at the same time need to restrict access to this resource. When you create a semaphore with CreateSemaphore () that is to be noted that while the maximum allowed count of resources currently available and resources count. Typically the current count is set to the maximum available resources to the resource counts, each additional thread access to shared resources, the resources currently available count is decremented by 1, as long as currently available resource count is greater than 0, the amount of the signal can be issued. However, when currently available count reduced to zero it indicates that the current resource occupation number of threads has reached the maximum number allowed, not allowed to enter the other thread, the semaphore signals in this case will not be sent. After processing thread shared resources, shall () function sets the current available resource count is incremented by 1 at the same time leaving ReleaseSemaphore. At any time of the currently available resources can never count greater than the maximum resource count.

Fourth, the event (Event)

    Event objects can also be used to synchronize threads through notification operation mode. And can implement different threads in the process synchronization.

V. Summary

  • Mutex and critical section is very similar to the role, but can be named mutex, which means it can use across process. So create a mutex requires more resources, so, if only for the internal process is critical zone will bring the advantage of speed and the ability to reduce resource use footprint with words. Because once the mutex mutex is cross-process is created, you can open it by name.
  • Mutex (Mutex), lights (Semaphore), event (Event) can be synchronized across processes using data manipulation, and other objects and data synchronization operation has nothing to do, but in terms of processes and threads, if processes and threads in the operating state, compared with no signal state, after the exit signaled state. So you can use WaitForSingleObject to wait for the process and thread to exit.
  • It can be specified by a mutex exclusive way to use resources, but if there is a case by the following mutex can not handle, such as now a user buys a database system three concurrent access licenses can be purchased according to the user access to the number of licenses determines how many threads / processes can perform database operations at the same time, this time using the mutex if there is no way to complete this requirement, semaphore object can be said to be a resource counter.

For a more detailed article on, look at this: http://www.cppblog.com/killsound/archive/2009/07/15/16147.html

Published 25 original articles · won praise 7 · views 2129

Guess you like

Origin blog.csdn.net/qq_41506111/article/details/102839206