Mutexes are also called mutexes, and the class and function declarations associated with mutexes in C++11 are in the <mutex> header file.
a <mutex> head file content introduction:Classes1.1 mutexes
Mutex Mutexclass (Class)
Recursive_mutex Recursivemutex Class (Class)
Time_mutex Timedmutex Class (Class)
Recursive_timed_mutex recursivetimed Murex (Class)
1.2 Locks
Lock_guard Lock_guard (classtemplate)
Unique_lock Unique_lock (classtemplate)
1.3 Other types
Once_flag flagargument type for call_once (class)
adopt_lock_t Typeof Adopt_lock (Class)
defer_lock_t Typeof Defer_lock (Class)
try_to_lock_t Typeof Try_to_lock (Class)
1.4 Functions
Try_lock Try to lock multiplemutexes (function template)
Lock Lockmultiple muteses (function template)
Call_once CallFunction once (public member function)
two Std::mutex
A mutex is a mutex object that, when a thread needs exclusive access to a resource, blocks other threads that need exclusive access to the same resource to access the resource at the same time.
Member function
(constructor) Constructmutex (public member function)
Lock lock Mutex (Publicmember function)
Try_lock Lockmutex if not locked (public member function)
Unlock Unlockmutex (public member function)
Native_handle Getnative handle (public member function)
Examples
#include <iostream> #include <thread> #include <mutex> std::mutexmtx; Define global mutex void Fucone (INTN, Charc) {//with mutex to std::cout signal exclusive access mtx.lock (); for (int i = 0; i < n;++i) { std::cout<< C; } std::cout<< Std::endl; Mtx.unlock ();} int main (int argc,_tchar* argv[]) { std::thread Th1 (fucone, +, ' # '); Std::thread Th2 (Fucone, +, ' $ '); Std::thread Th3 (Fucone, +, '% '); Std::thread Th4 (Fucone, +, ' ^ '); Th1.join (); Th2.join (); Th3.join (); Th4.join (); Return0;}
three Std::recursive_mutex
Std::recursive_mutex, like Std::mutex objects, are objects that can be locked and have the same member functions. Unlike Std::mutex, std::recursive allows the same thread to lock the mutex multiple times (that is, recursively locked) to obtain multiple levels of ownership of the mutex. Std::recursive_mutex releases the mutex with the same number of std::recursive_mutex::unlock () as the lock recursion depth. The number of lock () is the same as unlock (), except that it is roughly the same as the Std::mutex usage.
Four Std::time_mutex1 member function
(constructor) Constructmutex (public member function)
Lock lock Mutex (Publicmember function)
Try_lock Lockmutex if not locked (public member function)
Try_lock_for Tryto Lock for Time span (public member function)
Try_lock_until Tryto Lock until time point (public member function)
Unlock Unlockmutex (public member function)
Native_handle Getnative handle (public member function)
Try_lock_for Usage Note: It accepts a time range, indicating that the thread is blocked if it has not acquired a lock (unlike Std::mutex::try_lock, Try_lock is called if it does not acquire a lock and returns false directly). If a lock is acquired by another thread during this time, the thread can obtain a mutex lock and return FALSE if timeout (no lock is acquired within a specified period)
Try_lock_until Usage Note: It accepts a point in time as a parameter, and the thread is blocked if no lock is obtained before the specified point in time, if the lock is freed by another thread during this period, the thread can obtain a mutex lock if it expires (without acquiring a lock before the specified point in time). Returns false
Examples
#include <iostream> #include <chrono> #include <thread> #include <mutex> std::timed_mutextime_ MTx Voidfireworks () { //wait for the mutex lock period, every 200ms output character '-' while (!time_mtx.try_lock_for (Std::chrono::milliseconds (200)) { std::cout<< "-"; } When the mutex is acquired, this thread sleeps 1s after the output character "*" and Wraps std::this_thread::sleep_for (Std::chrono::milliseconds ()); std::cout<< "*" << Std::endl; Unlock time_mtx.unlock ();} int main (int argc,_tchar* argv[]) { std::thread threads[10]; Bind function for each thread fireworks for (auto&th:threads) { th= std::thread (fireWorks); } for (auto&th:threads) { th.join (); } Return0;}
Five Std::recusive_timed_mutex
the relationship between Std::recusive_timed_mutex and Std::timed_mutex is the same as the relationship between Std::recursive_mutex and Std::mutex, and there are not many introductions.
C++11 Multithreading--mutex Learning