首页 > 系统教程 > LINUX > 正文

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

月夜之吻
发布: 2025-02-20 14:22:01
原创
320人浏览过

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;
}
登录后复制

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

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号