Twelve windows critical section, a variety of other mutex

A, windows critical section

Similar to mutex == critical area.

 

Second, repeatedly enter the critical section

Enter the critical section (lock);

Leaving the critical section (unlocked);

With a thread critical section windows in the same critical section variable represents the entry (entercirticalsection) may be called multiple times (multiple entry), do not forget to enter several times, and left the critical area several times. c ++ 11 does not allow the same thread lock the same mutex multiple times, otherwise reported abnormal

 

Three, windows automatically destructor Technology

 

Fourth, the exclusive recursive mutex recursive_mutex

std :: mutex monopolize the mutex, when their own lock, others can not lock.

recursive_mutex: allows a single thread with a mutex repeatedly lock, and mutex usage of the same.

Low efficiency, more complex, the number of recursion is limited, too much may be reported abnormal

Fifth, the mutex with timeout std :: timed_mutex and std :: recursive_timed_mutex

Can not get past the lock, has been stuck, someone has been waiting to get to know the lock

With timeout is not get the lock, and so for some time, but also get to continue to take the following code.

:: lock_guard STD <STD :: recursive_mutex> sbguard (my_mutex); 
STD :: :: Chrono milliseconds timeout ( 100 ); // 100 ms 
IF (my_mutex.try_lock_for (timeout)) {
     // Wait 100 ms to get a lock
     // execute ,,,, 
    my_mutex.unlock (); 
} 
the else {
     // did not get a break 
    std :: :: Chrono microseconds sleeptime ( 100 ); // 100 subtle 
    std :: this_thread :: sleep_for (sleeptime) ; 
}

try_lock_for (): parameter is a period of time, is to wait for some time, if get a lock wait timeout or did not get that, you go;

try_lock_unit (): parameter is the future point in time, not to the period of time in this future time, if got the lock, the process to go down, if not to get that, but also to go down;

. 1 STD :: :: Chrono milliseconds timeout ( 100 ); // 100 msec 
2 IF (my_mutex.try_lock_unit (Chrono steady_clock :: :: now () + timeout)) 
{
// ........

}; // current time plus the timeout time

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11271167.html