首页主机资讯Ubuntu C++如何实现并发控制

Ubuntu C++如何实现并发控制

时间2025-12-15 10:38:04发布访客分类主机资讯浏览635
导读:在Ubuntu上使用C++实现并发控制,通常会涉及到多线程编程。C++11引入了标准线程库(<thread>),使得在C++中进行并发编程变得更加容易和安全。以下是一些基本的并发控制概念和如何在Ubuntu上使用C++实现它们的...

在Ubuntu上使用C++实现并发控制,通常会涉及到多线程编程。C++11引入了标准线程库(< thread> ),使得在C++中进行并发编程变得更加容易和安全。以下是一些基本的并发控制概念和如何在Ubuntu上使用C++实现它们的示例。

1. 创建线程

使用std::thread来创建一个新的线程。

#include <
    iostream>
    
#include <
    thread>


void helloFunction() {
    
    std::cout <
    <
     "Hello from a thread!" <
    <
     std::endl;

}


int main() {
    
    std::thread t(helloFunction);
    
    t.join();
     // 等待线程完成
    return 0;

}
    

2. 线程同步

为了避免多个线程同时访问共享资源导致的数据竞争,需要使用同步机制,比如互斥锁(std::mutex)。

#include <
    iostream>
    
#include <
    thread>
    
#include <
    mutex>
    

std::mutex mtx;
     // 创建一个互斥锁

void printMessage(const std::string&
 msg) {
    
    mtx.lock();
     // 锁定互斥锁
    std::cout <
    <
     msg <
    <
     std::endl;
    
    mtx.unlock();
 // 解锁互斥锁
}


int main() {
    
    std::thread t1(printMessage, "Hello from thread 1");
    
    std::thread t2(printMessage, "Hello from thread 2");
    

    t1.join();
    
    t2.join();
    

    return 0;

}
    

3. 条件变量

条件变量(std::condition_variable)可以用来同步线程间的操作,通常与互斥锁一起使用。

#include <
    iostream>
    
#include <
    thread>
    
#include <
    mutex>
    
#include <
    condition_variable>
    

std::mutex mtx;
    
std::condition_variable cv;
    
bool ready = false;


void printId(int id) {
    
    std::unique_lock<
    std::mutex>
     lck(mtx);

    cv.wait(lck, []{
    return ready;
}
    );
     // 等待直到ready为true
    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];
    
    // spawn 10 threads:
    for (int i = 0;
     i <
     10;
     ++i)
        threads[i] = std::thread(printId, i);
    

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

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

    return 0;

}
    

4. 原子操作

原子操作(std::atomic)可以用来进行无锁的并发编程,适用于简单的数据类型。

#include <
    iostream>
    
#include <
    thread>
    
#include <
    atomic>
    

std::atomic<
    int>
     counter(0);


void incrementCounter() {
    
    for (int i = 0;
     i <
     1000;
 ++i) {
    
        counter.fetch_add(1, std::memory_order_relaxed);

    }

}


int main() {
    
    std::thread t1(incrementCounter);
    
    std::thread t2(incrementCounter);
    

    t1.join();
    
    t2.join();
    

    std::cout <
    <
     "Counter value: " <
    <
     counter.load() <
    <
     std::endl;
    

    return 0;

}
    

编译和运行

在Ubuntu上编译C++程序时,需要链接线程库。可以使用g++编译器,并添加-pthread参数。

g++ -pthread your_program.cpp -o your_program
./your_program

这些是C++并发编程的一些基本概念和示例。在实际应用中,可能需要更复杂的同步和并发控制策略,这通常取决于具体的应用场景和需求。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu C++如何实现并发控制
本文地址: https://pptw.com/jishu/771431.html
Linux copendir函数如何实现目录遍历 Ubuntu C++怎样优化循环语句

游客 回复需填写必要信息