久综合色-久综合网-玖草影视-玖草资源在线-亚洲黄色片子-亚洲黄色片在线观看

Hello! 歡迎來到小浪云!


Linux下C++多線程同步怎么做


avatar
小浪云 2025-02-20 62

Linux下C++多線程同步怎么做

Linux環境下c++線程編程,線程同步至關重要。本文將介紹幾種常用的同步方法:

一、互斥鎖 (Mutex)

互斥鎖是基礎的同步機制,用于保護共享資源,防止數據競爭。

#include <iostream> #include <thread> #include <mutex>  std::mutex mtx; // 全局互斥鎖  void print_block(int n, char c) {     mtx.lock(); // 加鎖     for (int i = 0; i < n; ++i) {         std::cout << c;     }     mtx.unlock(); // 解鎖 }  int main() {     std::thread th1(print_block, 50, '*');     std::thread th2(print_block, 50, '$');      th1.join();     th2.join();      return 0; }

二、條件變量 (Condition Variable)

立即學習C++免費學習筆記(深入)”;

條件變量實現線程間的等待和通知。

#include <iostream> #include <thread> #include <mutex> #include <condition_variable>  std::mutex mtx; std::condition_variable cv; bool ready = false;  void print_id(int id) {     std::unique_lock<std::mutex> lck(mtx);     cv.wait(lck, []{return ready;}); // 等待條件滿足     std::cout << "thread " << id << std::endl; }  void go() {     std::lock_guard<std::mutex> lck(mtx);     ready = true;     cv.notify_all(); // 通知所有等待線程 }  int main() {     std::thread threads[10];     for (int i = 0; i < 10; ++i) {         threads[i] = std::thread(print_id, i);     }     go();     for (auto& th : threads) {         th.join();     }     return 0; }

三、信號量 (Semaphore)

信號量是更高級的同步機制,控制對共享資源的訪問次數。

#include <iostream> #include <thread> #include <semaphore>  std::binary_semaphore sem(0); // 二進制信號量  void print_block(int n, char c) {     sem.acquire(); // 等待信號量     for (int i = 0; i < n; ++i) {         std::cout << c;     } }  void go() {     std::this_thread::sleep_for(std::chrono::seconds(1)); // 模擬任務     sem.release(); // 釋放信號量 }  int main() {     std::thread th1(print_block, 50, '*');     std::thread th2(print_block, 50, '$');     std::thread t(go);      th1.join();     th2.join();     t.join();      return 0; }

四、原子操作 (Atomic Operations)

原子操作無需鎖即可保證線程安全。

#include <iostream> #include <thread> #include <atomic>  std::atomic<int> counter(0);  void increment() {     for (int i = 0; i < 100000; ++i) {         ++counter;     } }  int main() {     std::thread t1(increment);     std::thread t2(increment);      t1.join();     t2.join();      std::cout << counter << std::endl;     return 0; }

五、屏障 (Barrier)

屏障確保多個線程在特定點同步。

#include <iostream> #include <thread> #include <barrier>  std::barrier bar(2); // 創建一個屏障,等待兩個線程  void print_hello() {     std::cout << "Hello ";     bar.wait(); // 等待屏障     std::cout << "World!" << std::endl; }  int main() {     std::thread t1(print_hello);     std::thread t2(print_hello);      t1.join();     t2.join();      return 0; }

選擇合適的同步機制取決于具體應用場景。 以上示例代碼僅供參考,實際應用中可能需要更復雜的同步策略。

相關閱讀

主站蜘蛛池模板: 日韩毛片高清在线看 | 久久国产午夜精品理论片34页 | 亚洲欧美日韩成人一区在线 | 亚洲一区二区三区成人 | 综合自拍亚洲综合图区美腿丝袜 | 国产欧美日韩不卡在线播放在线 | 精品伊人久久久久网站 | 亚洲第一网色综合久久 | 亚洲国产精品日韩高清秒播 | 亚洲视频播放 | 五月久久亚洲七七综合中文网 | 亚洲午夜网站 | 一级特黄aaa大片免费看 | 亚洲第一区视频在线观看 | 国产精品91在线播放 | 可以免费看黄色的网站 | 久久99久久精品久久久久久 | 久久久久国产 | 欧美三级香港三级日本三级 | 亚洲视频免费播放 | 日韩一级黄色 | 欧美一级特黄aaaaaa在线看首页 | 国产一区二区三区不卡在线观看 | 欧美色xxx| 国产欧美日韩综合精品一区二区 | 亚洲乱视频 | 欧美日韩一区二区三区视频在线观看 | 亚洲国产高清在线精品一区 | 日韩有码第一页 | 亚洲精品资源在线 | cao在线| 免费观看欧美精品成人毛片能看的 | 国产成人精品日本亚洲语音2 | 高清国产在线观看 | 在线免费看a| 日本人成在线视频免费播放 | 一级做性色a爱片久久片 | 日韩三级一区 | 久久精品国产99精品最新 | 成人性一级视频在线观看 | 久久伊人精品热在75 |