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

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; }

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

相關閱讀

主站蜘蛛池模板: 亚洲国产成人久久午夜 | 欧美成人乱弄视频 | 国产成人精品一区二区免费 | 亚洲视频在线a视频 | 美国免费一级片 | 中文字幕人成乱码在线观看 | 欧美性群另类交 | 99爱视频99爱在线观看免费 | 久久91精品国产91久久 | 欧美日韩在线观看一区二区 | 毛片直接看 | 免费观看视频成人国产 | 模特三级在线观看 | 国产区亚洲区 | ffyybb免费福利视频 | 免费观看呢日本天堂视频 | 久久riav.com| 免费被黄网站在观看 | 在线观看毛片视频 | 黄色网址网站在线观看 | 国产成人免费手机在线观看视频 | 丝袜美腿在线不卡视频播放 | 美女扒开腿让男人桶爽免费动态图 | 一级片免费在线 | 日韩欧美精品综合一区二区三区 | 在线播放一级片 | 99re热久久| 91九色偷拍 | 岛国伊人 | 亚洲国产欧美日韩第一香蕉 | 美女一级ba大片免色野外 | 午夜在线观看视频免费 成人 | 欧美久草视频 | 台湾三级毛片 | 中文字幕有码在线 | 中文字幕亚洲在线 | 台湾三级 | 最近中文字幕在线 | 中文 | 天堂一区二区在线观看 | 欧美三级黄 | 巨乳毛片|