Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Linux下C++多线程同步怎么做_创想鸟

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

linux下c++多线程同步怎么做

Linux环境下C++多线程编程,线程同步至关重要。本文将介绍几种常用的同步方法:

一、互斥锁 (Mutex)

互斥锁是基础的同步机制,用于保护共享资源,防止数据竞争。

#include #include #include 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 #include #include #include std::mutex mtx;std::condition_variable cv;bool ready = false;void print_id(int id) {    std::unique_lock lck(mtx);    cv.wait(lck, []{return ready;}); // 等待条件满足    std::cout << "thread " << id << std::endl;}void go() {    std::lock_guard 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)

豆包AI编程 豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483 查看详情 豆包AI编程

信号量是更高级的同步机制,控制对共享资源的访问次数。

#include #include #include 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 #include #include std::atomic 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 #include #include 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;}

选择合适的同步机制取决于具体应用场景。 以上示例代码仅供参考,实际应用中可能需要更复杂的同步策略。

以上就是Linux下C++多线程同步怎么做的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/561355.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Docker是什么
上一篇 2025年11月10日 01:15:10
安卓手机如何避免误触操作?_诺基亚安卓手机触控灵敏度调整
下一篇 2025年11月10日 01:15:18

相关推荐

发表回复

登录后才能评论
关注微信