C++11多線程——mutex學習

Mutex也稱之爲互斥量,C++11中與mutex相關的類與函數聲明都在<mutex>頭文件中。

 

一<mutex>頭文件內容簡介:

Classes
1.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——Flag argument type for call_once(class)

adopt_lock_t——ypeof adopt_lock(class)

defer_lock_t——Type of defer_lock(class)

try_to_lock_t——ypeof try_to_lock(class)

 

1.4 Functions

try_lock——Try to lock multiple mutexes(function template)

lock——Lock multiple mutexes(function template)

call_once—— Call function once(public member function)

 

二 std::mutex

       Mutex是一個互斥鎖對象,當某線程需要對資源獨佔訪問時,阻止需要獨佔訪問相同資源的其他線程同時對資源進行訪問。

Member function

(constructor)——Construct mutex(public member function)

Lock——Lock mutex(publicmember function)

try_lock——Lock mutex if not locked(public member function)

unlock——Unlock mutex(public member function)

native_handle——Get native handle(public member function)

 

Examples

 

#include <iostream>
#include <thread>
#include <mutex>
 
std::mutexmtx;                         //定義全局互斥量
 
void fucOne(intn, charc)
{//用mutex對std::cout signal 獨佔訪問
       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, 50, '#');
       std::thread th2(fucOne, 50, '$');
       std::thread th3(fucOne, 50, '%');
       std::thread th4(fucOne, 50, '^');
 
       th1.join();
       th2.join();
       th3.join();
       th4.join();
 
       return0;
}


 

 

三 std::recursive_mutex

              std::recursive_mutex 與 std::mutex 對象一樣,都是一種可以上鎖的對象,且成員函數相同。與std::mutex不同的是,std::recursive允許同一個線程對互斥量多次上鎖(即遞歸上鎖),以獲取互斥量的多層所有權。std::recursive_mutex釋放互斥量時需要調用與該鎖遞歸深度相同次數的std::recursive_mutex::unlock()。即lock()次數與unlock()次數相同,除此之外,其與std::mutex用法大致相同。

      

四  std::time_mutex

    1   member function

(constructor)——Construct mutex(public member function)

Lock——Lock mutex(publicmember function)

try_lock——Lock mutex if not locked(public member function)

try_lock_for——Try to lock for time span(public member function)

try_lock_until——Try to lock until time point(public member function)

unlock——Unlock mutex(public member function)

native_handle——Get native handle(public member function)

       try_lock_for用法說明:其接受一個時間範圍,表示在這一段時間範圍之內線程如果沒有獲得鎖則被阻塞住(與std::mutex::try_lock不同,try_lock被調用時如果沒有獲得鎖,直接返回false),如果在此期間其他線程獲得了鎖,則線程可獲得互斥量的鎖,如果超時(在指定時間內沒有獲得鎖),返回false

       try_lock_until用法說明:其接受一個時間點作爲參數,在指定時間點未到來之前線程如果沒有獲得鎖則被阻塞,如果在此期間其他線程釋放了鎖,則該線程可獲得互斥量的鎖,如果超時(在指定時間點到來之前沒有獲得鎖),返回false

Examples

#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
 
std::timed_mutextime_mtx;
 
voidfireWorks()
{
       //等待獲取互斥鎖期,每200ms輸出字符‘-’
       while(!time_mtx.try_lock_for(std::chrono::milliseconds(200)))
       {
              std::cout<< "-";
       }
 
       //獲取到互斥鎖時,此線程睡眠1s之後輸出字符“*”並換行
       std::this_thread::sleep_for(std::chrono::milliseconds(1000));
       std::cout<< "*" << std::endl;
 
       //解鎖
       time_mtx.unlock();
}
 
 
int main(int argc,_TCHAR* argv[])
{
       std::thread threads[10];
 
       //爲每個線程綁定函數fireWorks
       for(auto&th:threads)
       {
              th= std::thread(fireWorks);
       }
 
       for(auto&th : threads)
       {
              th.join();
       }
       return0;
}


 

五  std::recusive_timed_mutex

       std::recusive_timed_mutex與std::timed_mutex的關係跟std::recursive_mutex與std::mutex的關係一樣,這裏不多做介紹。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章