ubuntu

C++程序在Ubuntu如何实现并发控制

小樊
44
2025-12-28 21:24:08
栏目: 编程语言

在Ubuntu上使用C++实现并发控制,通常会涉及到多线程编程。C++11标准引入了<thread>库,它提供了一些工具来创建和管理线程。此外,还有其他同步机制,如互斥锁(<mutex>)、条件变量(<condition_variable>)、原子操作(<atomic>)等,用于控制对共享资源的访问。

以下是一个简单的例子,展示了如何在C++中使用<thread><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;
    }
    std::cout << '\n';
    mtx.unlock(); // 解锁
}

int main() {
    std::thread th1(print_block, 50, '*');
    std::thread th2(print_block, 50, '$');

    th1.join(); // 等待线程th1完成
    th2.join(); // 等待线程th2完成

    return 0;
}

在这个例子中,我们定义了一个print_block函数,它会打印一定数量的字符。为了避免两个线程同时打印导致输出混乱,我们使用了std::mutex来确保同一时间只有一个线程可以执行打印操作。

除了互斥锁,C++还提供了其他同步原语,例如:

下面是使用std::lock_guardstd::condition_variable的一个例子:

#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;}); // 等待直到ready为true
    std::cout << "Thread " << id << '\n';
}

void go() {
    std::lock_guard<std::mutex> lck(mtx);
    ready = true;
    cv.notify_all(); // 通知所有等待的线程
}

int main() {
    std::thread threads[10];
    // spawn 10 threads:
    for (int i = 0; i < 10; ++i)
        threads[i] = std::thread(print_id, i);

    std::cout << "10 threads ready to race...\n";
    go(); // go!

    for (auto &th : threads) th.join();

    return 0;
}

在这个例子中,我们创建了10个线程,它们都在等待一个信号。主线程通过调用go函数来发出这个信号,使得所有等待的线程可以继续执行。

为了编译上述代码,你需要使用支持C++11或更高标准的编译器,并且在编译命令中添加-std=c++11(或更高版本)标志。例如:

g++ -std=c++11 -pthread your_program.cpp -o your_program

注意,-pthread标志是必须的,因为它会定义预处理器宏_REENTRANT并链接POSIX线程库。

0
看了该问题的人还看了