您好,登錄后才能下訂單哦!
小編給大家分享一下C++中Boost多線程、線程同步的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
線程的創建 boost_thread,boost_system 多線程的創建 線程的參數傳遞 線程的創建方式 線程的join 加入join,回收線程 線程中斷 線程中斷2, 線程組 boost 線程的死鎖 boost 線程遞歸鎖 線程互斥鎖,線程同步 unique_lock 鎖,離開作用域自動釋放 unique_lock 鎖 示例 2,可以顯式的釋放鎖 boost 1次初始化 boost 條件變量 boost 線程鎖,一個賬戶往另外一個賬戶轉錢案例 boost upgrade_lock
知識背景:
理解什么是線程,什么是進程,區別是什么,如何使用多進程多線程
線程的創建 boost_thread,boost_system
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun() { cout << "Hello Boost threads !" << endl; } int main() { boost::thread t1(fun); t1.join(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out Hello Boost threads ! chunli@Linux:~/boost$
多線程的創建
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun1(){cout << "Hello Boost threads 1!" << endl;} void fun2(){cout << "Hello Boost threads 2!" << endl;} void fun3(){cout << "Hello Boost threads 3!" << endl;} int main() { boost::thread t1(fun1); t1.join(); boost::thread t2(fun2); t2.join(); boost::thread t3(fun3); t3.join(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out Hello Boost threads 1! Hello Boost threads 2! Hello Boost threads 3! chunli@Linux:~/boost$
線程的參數傳遞
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun1(const int &id){cout << "threads id "<<id << endl;} void fun2(const int &id){cout << "threads id "<<id << endl;} void fun3(const int &id){cout << "threads id "<<id << endl;} int main() { boost::thread t1(fun1,1); t1.join(); boost::thread t2(fun2,2); t2.join(); boost::thread t3(fun3,3); t3.join(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 threads id 2 threads id 3 chunli@Linux:~/boost$
線程的創建方式
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun1(const int &id) { cout << "threads id "<<id << endl; } struct MyThread { void operator()(const int &id) { cout << "threads id "<<id << endl; } void fun(const int &id) { cout << "threads id "<<id << endl; } }; int main() { boost::thread t1(fun1,1);//自由函數 t1.join(); MyThread mythread; boost::thread t2(mythread,2);//函數對象 t2.join(); boost::thread t3(&MyThread::fun,mythread,3); //成員函數 t3.join(); boost::thread t4(MyThread(),4); //臨時對象 t4.join(); boost::thread t5(boost::ref(mythread),5);//對象引用 t5.join(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 threads id 2 threads id 3 threads id 4 threads id 5 chunli@Linux:~/boost$
線程的join
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun1(const int &id){cout << "threads id "<<id << endl;} int main() { boost::thread t1(fun1,1); //t1.join(); cout << "main end!" << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end! chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end!threads id 1 chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end! threads id 1 chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end! threads id 1chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end! threads id 1 chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out main end! threads id 1 chunli@Linux:~/boost$ 可以看出,如果沒有join的等待,結果是不可預期的.
加入join,回收線程
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; void fun1(const int &id){cout << "threads id "<<id << endl;} int main() { boost::thread t1(fun1,1); t1.join(); cout << "main end!" << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 main end! chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 main end! chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 main end! chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out threads id 1 main end! chunli@Linux:~/boost$
線程中斷
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; void f1(const int& id) { cout << "thread #" << id << ": started" << endl; boost::system_time const timeout = boost::get_system_time()+ boost::posix_time::seconds(3); thread::sleep(timeout);//sleep不會放棄時間片 cout << "thread #" << id << ": ended" << endl; } void f2(const int& id) { cout << "thread #" << id << ": started" << endl; thread::yield();//預定義中斷點.主動放棄時間片 cout << "thread #" << id << ": ended" << endl; } void f3(const int& id) { cout << "thread #" << id << ": started" << endl; boost::this_thread::interruption_point();//預定義中斷點 cout << "thread #" << id << ": ended" << endl; } int main() { thread t1(f1, 1); t1.interrupt(); thread t2(f2, 2); thread t3(f3, 3); t3.interrupt(); t1.join(); t2.join(); t3.join(); } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out thread #2: started thread #1: started thread #3: started thread #2: ended chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out thread #1: started thread #3: started thread #2: started thread #2: ended chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out thread #thread #2: started1: started thread #3: started thread #2: ended chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out thread #3: started thread #1: started thread #2: started thread #2: ended chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out thread #2: started thread #3: started thread #thread #2: ended 1: started chunli@Linux:~/boost$ 只有2線程不會被打斷
線程中斷2,
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; void print(const int& id) { boost::this_thread::disable_interruption di;//創建一個不可被打斷的對象 cout << boost::this_thread::interruption_enabled() << endl; cout << "thread #" << id << ": "; //boost::this_thread::sleep(boost::posix_time::seconds(2)); boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(2); thread::sleep(timeout); for (int i = 1; i < 11; ++i) { cout << i << ' '; } cout << endl; boost::this_thread::restore_interruption ri(di);//到這里,對象不可被打斷 cout << boost::this_thread::interruption_enabled() << endl; //實際上,是可以被打斷 } int main() { //線程還沒有運行結束,叫被打斷 thread t1(print, 1); thread t2(print, 2); thread t3(print, 3); t3.interrupt(); t1.join(); t2.join(); t3.join(); } chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 0 thread #1: 0 thread #3: 0 thread #2: 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 1 chunli@Linux:~/boost$ g++ main.cpp -l boost_thread -l boost_system && ./a.out 0 thread #1: 0 thread #2: 0 thread #3: 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 1 chunli@Linux:~/boost$
線程組
chunli@Linux:~/桌面/qt_pro/01/untitled$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; void f1(){ cout << "fun1 " << endl;} void f2(){ cout << "fun2 " << endl;} int main() { boost::thread_group group; for(int i = 0;i<3;++i) { group.create_thread(f1); } group.add_thread(new boost::thread(f2)); cout<<group.size()<<endl; group.join_all(); } chunli@Linux:~/桌面/qt_pro/01/untitled$ g++ main.cpp -lboost_thread -lboost_system -Wall && ./a.out fun1 4 fun1 fun2 fun1 chunli@Linux:~/桌面/qt_pro/01/untitled$ g++ main.cpp -lboost_thread -lboost_system -Wall && ./a.out fun1 fun1 fun1 4 fun2 chunli@Linux:~/桌面/qt_pro/01/untitled$
boost 線程的死鎖
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::mutex m; void function1() { m.lock(); cout << "function 1 \n"; m.unlock(); } void function2() { m.lock(); cout << "function 2 \n"; function1(); m.unlock(); } int main() { thread t1(function1); t1.join(); thread t2(function2); t2.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system&& ./a.out function 1 function 2 ^C chunli@Linux:~/boost$
boost 線程遞歸鎖
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::recursive_mutex m; void function1() { m.lock(); cout << "function 1 \n"; m.unlock(); } void function2() { m.lock(); cout << "function 2 \n"; function1(); m.unlock(); } int main() { thread t1(function1); t1.join(); thread t2(function2); t2.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out function 1 function 2 function 1 chunli@Linux:~/boost$
線程互斥鎖,線程同步
boost::mutex m; void function1(int id) { m.lock(); cout <<"thread #"<<id<<":"; for(int i=0;i<15;i++) cout << i<<' '; cout << endl; m.unlock(); } int main() { thread t1(function1,1); t1.join(); thread t2(function1,2); t2.join(); thread t3(function1,3); t3.join(); thread t4(function1,4); t4.join(); thread t5(function1,5); t5.join(); thread t6(function1,6); t6.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out thread #1:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 thread #2:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 thread #3:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 thread #4:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 thread #5:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 thread #6:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 chunli@Linux:~/boost$
unique_lock 鎖,離開作用域自動釋放
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <list> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::mutex m; int k = 0; void decrement() { boost::unique_lock<boost::mutex> lock(m); for(int i = 0;i<=100;++i) { k-=i; } cout << "after decrement k="<<k << endl; } void increment() { boost::unique_lock<boost::mutex> lock(m); for(int i = 0;i<=100;++i) { k+=i; } cout << "after increment k="<<k << endl; } int main() { boost::thread t1(increment); t1.join(); boost::thread t2(decrement); t2.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out after increment k=5050 after decrement k=0 chunli@Linux:~/boost$
unique_lock 鎖 示例 2,可以顯式的釋放鎖
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <vector> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::mutex m; void updateString() { boost::unique_lock<boost::mutex> lock(m);//lock lock.unlock();//unlock lock.lock(); } int main() { thread t1(updateString); t1.join(); thread t2(updateString); t2.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out chunli@Linux:~/boost$
boost 1次初始化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::once_flag once = BOOST_ONCE_INIT; // 注意這個操作不要遺漏了 void func() { cout << "Will be called but one time!" << endl; } void threadFunc() { // func(); boost::call_once(&func, once); } int main() { boost::thread_group threads; for (int i = 0; i < 5; ++i) threads.create_thread(&threadFunc); threads.join_all(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system && ./a.out Will be called but one time! chunli@Linux:~/boost$
boost 條件變量
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::condition_variable cond; // 關聯多個線程的條件變量 boost::mutex m; // 保護共享資源 k 的互斥體 int k = 0; // 共享資源 void f1(const int& id) { boost::unique_lock<boost::mutex> lock(m); while (k < 5) { cout << "thread #" << id << ": k < 5, waiting ..." << endl; cond.wait(lock); // #1 } cout << "thread #" << id << ": now k >= 5, printing ..." << endl; } void f2(const int& id) { boost::unique_lock<boost::mutex> lock(m); cout << "thread #" << id << ": k will be changed ..." << endl; k += 5; cond.notify_all(); // #2 不需lock } int main() { // 如果f2()中是 cond.notify_one(),結果? boost::thread t1(f1, 1); boost::thread t2(f1, 2); boost::thread t3(f2, 100); t1.join(); t2.join(); t3.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system && ./a.out thread #1: k < 5, waiting ... thread #2: k < 5, waiting ... thread #100: k will be changed ... thread #1: now k >= 5, printing ... thread #2: now k >= 5, printing ... chunli@Linux:~/boost$
boost 線程鎖,一個賬戶往另外一個賬戶轉錢案例
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; class Account { boost::mutex m; double balance; public: Account() : balance() { } Account(const double& bal) : balance(bal) { } double getBalance() const { return balance; } friend void transfer(Account& from, Account& to, double amount); }; //// version 3: OK (使用lock() 和 unique_lock) //void transfer(Account& from, Account& to, double amount) { // boost::lock(from.m, to.m); // boost::unique_lock<boost::mutex> lockFrom(from.m, boost::adopt_lock); // boost::unique_lock<boost::mutex> lockTo(to.m, boost::adopt_lock); // // from.balance -= amount; // to.balance += amount; //} // version 2: OK (使用lock() 和 lock_guard) void transfer(Account& from, Account& to, double amount) { boost::lock(from.m, to.m); boost::lock_guard<boost::mutex> lockFrom(from.m, boost::adopt_lock); boost::this_thread::sleep(boost::posix_time::seconds(1)); boost::lock_guard<boost::mutex> lockTo(to.m, boost::adopt_lock); from.balance -= amount; to.balance += amount; } // version 1: 可能造成死鎖 //void transfer(Account& from, Account& to, double amount) { // boost::lock_guard<boost::mutex> lockFrom(from.m); // #1 // boost::this_thread::sleep(boost::posix_time::seconds(1)); // boost::lock_guard<boost::mutex> lockTo(to.m); // #2 // from.balance -= amount; // to.balance += amount; //} int main() { Account a1(1200.00); Account a2(300.00); boost::thread t1(transfer, boost::ref(a1), boost::ref(a2), 134.85); boost::thread t2(transfer, boost::ref(a2), boost::ref(a1), 100.30); t1.join(); t2.join(); cout << "Balance of a1: " << a1.getBalance() << endl; cout << "Balance of a2: " << a2.getBalance() << endl; } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out Balance of a1: 1165.45 Balance of a2: 334.55 chunli@Linux:~/boost$
boost upgrade_lock
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; boost::shared_mutex m; int k = 1; void f(int id) { boost::upgrade_lock<boost::shared_mutex> lock(m); cout << "thread #" << id << ": " << k << endl; if (k < 6) { // boost::unique_lock<boost::shared_mutex> lock2(boost::move(lock)); // alternate: boost::upgrade_to_unique_lock<boost::shared_mutex> lock2(lock); k += 3; } } int main() { boost::thread t1(f, 1); boost::thread t2(f, 2); boost::thread t3(f, 3); t1.join(); t2.join(); t3.join(); } chunli@Linux:~/boost$ g++ main.cpp -lboost_thread -lboost_system -lpthread && ./a.out thread #2: 1 thread #1: 4 thread #3: 7 chunli@Linux:~/boost$
以上是“C++中Boost多線程、線程同步的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。