日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

boost--线程同步

發(fā)布時(shí)間:2024/10/12 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 boost--线程同步 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?1、互斥鎖(互斥量)

? mutex是獨(dú)占式的互斥鎖。timed_mutex增加了超時(shí)功能。

? 成員函數(shù):lock()用于鎖定,try_lock()為非阻塞版本的鎖定,unlock()用于解鎖。timed_lock()只屬于timed_mutex,它可以等待一定的時(shí)間,等待的時(shí)間可以是一個(gè)時(shí)間段,也可以是指定的時(shí)間。

??使用方法:使用mutex必須配合try-catch塊以保證解鎖互斥量,eg:

#include "boost\thread.hpp"int main() {boost::mutex mu;try{mu.lock();cout << "Need to be protected" << endl; //io流是個(gè)共享資源,多線程內(nèi)使用的話需要同步 mu.unlock();}catch (...){mu.unlock();}return 0; } View Code

? mutex還提供了一系列的RAII型的互斥鎖,用于取消麻煩的try-catch塊,它會(huì)在構(gòu)造的時(shí)候鎖定互斥量,析構(gòu)時(shí)自動(dòng)解鎖,eg:

#include "boost\thread.hpp"int main() {boost::mutex mu;boost::mutex::scoped_lock lock(mu);cout << "Need to be protected" << endl; //io流是個(gè)共享資源,多線程內(nèi)使用的話需要同步return 0; } View Code

? 使用實(shí)例:以下定義了一個(gè)支持原子前++的計(jì)數(shù)器atom_increase:

#include "boost\thread.hpp"template<typename T> class atom_increase { public:atom_increase(T x = 0): n(x){} //轉(zhuǎn)換構(gòu)造函數(shù),實(shí)現(xiàn)T類型到atom_increase類型的隱式轉(zhuǎn)換 T operator++() //重載前++運(yùn)算符 {boost::mutex::scoped_lock lock(mu);return ++n;}operator T() { return n; } //類型轉(zhuǎn)換函數(shù),實(shí)現(xiàn)atom_increase類型到T類型的隱式轉(zhuǎn)換private:T n;boost::mutex mu; };int main() { atom_increase<int> n = 0; //隱式轉(zhuǎn)換:int -> atom_increase++n; //原子前++int i = 5 + n; //隱式轉(zhuǎn)換:atom_increase ->intreturn 0; } View Code

2、遞歸鎖

? recursive_mutex是遞歸鎖,可以多次鎖定,相應(yīng)的也要多次解鎖。recursive_timed_mutex增加了超時(shí)功能。遞歸鎖的使用接口跟互斥鎖基本相同。

3、讀寫鎖

? shared_mutex是讀寫鎖,提供了multiple-reader / single-writer功能。讀取鎖定時(shí)我們使用shared_lock<shared_mutex>對(duì)象,寫入鎖定時(shí)我們使用unique_lock<shared_mutex>對(duì)象:

boost::shared_mutex rw_mu;//read thread {boost::shared_lock<boost::shared_mutex> sl(rw_mu); //讀鎖定//...... }//write thread {boost::unique_lock<boost::shared_mutex> ul(rw_mu); //寫鎖定//......} View Code

?4、條件變量

? condition_variable_any是條件變量,它用來在一個(gè)線程中等待某個(gè)事件的發(fā)生(滿足某個(gè)條件),另一個(gè)線程會(huì)使條件成立。條件變量需要與一個(gè)互斥量配合使用。condition_variable_any::wait()用來等待條件滿足,wait_for()用來等待條件滿足直到超時(shí),wait_until()用來等待條件滿足直到指定的時(shí)間, condition_variable_any::notify_one() / notify_all()用來在條件滿足的時(shí)候通知條件變量。

??boost中的條件變量的使用方法與posix或windows下條件變量使用方法基本一致:

#include "boost\thread.hpp"boost::condition_variable_any g_cd; boost::mutex g_mu; bool g_bConditionFlag = false;void Thread1Proc() {boost::mutex::scoped_lock lock(g_mu);while (!g_bConditionFlag){boost::this_thread::sleep(boost::posix_time::seconds(1));g_cd.wait(g_mu);}printf("thread1 exit\n"); }int main() {boost::thread t(Thread1Proc);t.detach();boost::this_thread::sleep(boost::posix_time::milliseconds(100));g_mu.lock();g_bConditionFlag = true;g_cd.notify_one();g_mu.unlock();printf("here\n");getchar();return 0; } View Code

?5、barrier

? barrier稱為護(hù)欄,它可以用來設(shè)置線程執(zhí)行到barrier時(shí)必須等待,直到所有線程都達(dá)到這個(gè)點(diǎn)時(shí)才繼續(xù)執(zhí)行。

?6、C++11中的線程操作

? c++11也提供了創(chuàng)建線程和線程同步的方法,c++11里的mutex,與boost里的mutex用法類似:

#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex, std::lock std::mutex foo, bar;void task_a() {// foo.lock(); // bar.lock();std::lock(foo, bar);std::cout << "task a\n";foo.unlock();bar.unlock(); }void task_b() {// bar.lock(); // foo.lock();std::lock(bar, foo);std::cout << "task b\n";bar.unlock();foo.unlock(); }int main() {std::thread th1(task_a);std::thread th2(task_b);// th1.detach();// th2.detach(); th1.join();th2.join();return 0; } View Code

? 創(chuàng)建線程的時(shí)候需要注意三點(diǎn):

? ①、如果使用函數(shù)對(duì)象作為thread的參數(shù)的話,直接傳入臨時(shí)對(duì)象會(huì)出錯(cuò),可以定義一個(gè)對(duì)象傳入或者使用lambda表達(dá)式:

class CTask { public:void operator()(){int a = 0;} };//std::thread th1(CTask()); //直接傳入臨時(shí)對(duì)象會(huì)出錯(cuò) CTask task; std::thread th1(task); th1.join(); View Code

? ②、傳遞給線程函數(shù)的參數(shù)是先保存在于一個(gè)中轉(zhuǎn)站中,當(dāng)函數(shù)執(zhí)行的時(shí)候再傳給函數(shù)的形參,而這個(gè)時(shí)候傳遞的參數(shù)指向的值很有可能已經(jīng)失效,所以,對(duì)于線程函數(shù)傳遞的參數(shù)應(yīng)該與形參類型相同,而不是再進(jìn)行轉(zhuǎn)換。

? ③、如果線程函數(shù)的參數(shù)是引用的話傳入時(shí)還需要結(jié)合ref來使用。

void task(int& a, string str){}int iNum = 0;char* pStr = new char[100];strcpy(pStr, "test");//std::thread th(task, 5, std::ref(iNum), pStr); //不應(yīng)該直接傳入pStrstd::thread th(task, std::ref(iNum), string(pStr)); //應(yīng)該傳入對(duì)應(yīng)類型delete[] pStr;th.join(); View Code

??c++中的lock_guard、unique_lock也是RAII型的互斥鎖,類似boost的scoped_lock,使用示例:

std::lock_guard<std::mutex> _lock(m_mutex);g_num++;

??unique_lock在lock_guard的基礎(chǔ)上增加了加鎖和解鎖的接口方法,對(duì)比下面的fun1和fun2,二者效果相同,通過代碼可以看出二者區(qū)別:

std::mutex g_mu;void fun1() {{std::lock_guard<std::mutex> guard(g_mu);//do something 1 }//do something 2 {std::lock_guard<std::mutex> guard(g_mu);// do something 3 } }void fun2() {std::unique_lock<std::mutex> guard(g_mu);//do something 1 guard.unlock();//do something 2 guard.lock();// do something 3 } View Code

??unique_lock可以配合條件變量來使用,而lock_guard不能,因?yàn)閘ock_guard沒有加鎖和解鎖的接口方法。另外,unique_lock和lock_guard都不能復(fù)制,unique_lock可以移動(dòng),lock_guard不能移動(dòng)。c++11中的條件變量condition_variable與boost的使用類似,其接口方法有wait()、notify_one()等。

?

轉(zhuǎn)載于:https://www.cnblogs.com/milanleon/p/7591530.html

總結(jié)

以上是生活随笔為你收集整理的boost--线程同步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。