色偷偷91综合久久噜噜-色偷偷成人-色偷偷尼玛图亚洲综合-色偷偷人人澡久久天天-国内精品视频一区-国内精品视频一区二区三区

Hello! 歡迎來到小浪云!


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


avatar
小浪云 2025-02-20 88

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

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

相關閱讀

主站蜘蛛池模板: 四虎影视库国产精品一区 | 97青草最新免费精品视频 | 国产一区亚洲欧美成人 | 日本欧美一区二区三区在线 | 一级黄色大毛片 | 亚洲婷婷网 | 中国黄色在线观看 | 精品国产福利一区二区在线 | 婷婷激情四射网 | 老司机av | 亚洲精品成人久久久影院 | 日日做日日摸夜夜爽 | 日本黄色免费看 | 国产精品亚洲天堂 | 久久伊人网站 | 中文字幕 一区 婷婷 在线 | 欧美—级v免费大片 | 天天影视色综合 | 四虎影院永久免费观看 | 中国三级毛片 | 一区二区在线播放视频 | 黄色三级免费观看 | 午夜女上男下xx00xx00动态 | 国产三香港三韩国三级不卡 | 亚洲射| 三级国产三级在线 | 404毛片| 亚洲 欧美 精品专区 极品 | 亚洲国产精品一区二区久久 | 久久久久88色偷偷免费 | 日韩伦理剧在线观看 | 一级性毛片 | 中文字幕久精品免费视频 | 精品午夜久久影视 | 国产三级日产三级韩国三级 | 色狠狠狠色噜噜噜综合网 | 欧美一级黄视频 | 五月网婷婷 | 天天操天天射天天操 | 天天干狠狠操 | 精品久久一区二区三区 |