温馨提示×

Linux C++中如何处理多线程同步问题

小樊
45
2025-08-03 22:54:12
栏目: 编程语言

在Linux环境下使用C++进行多线程编程时,处理线程同步是一个重要的问题。线程同步的目的是确保多个线程能够安全地访问共享资源,避免数据竞争和不一致性。以下是一些常用的线程同步机制:

1. 互斥锁(Mutex)

互斥锁是最基本的同步机制之一,用于保护共享资源。

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

std::mutex mtx; // 全局互斥锁
int shared_data = 0;

void increment() {
    mtx.lock(); // 加锁
    ++shared_data;
    mtx.unlock(); // 解锁
}

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

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

    std::cout << "Shared data: " << shared_data << std::endl;
    return 0;
}

2. 递归互斥锁(Recursive Mutex)

递归互斥锁允许同一个线程多次锁定同一个互斥锁,而不会导致死锁。

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

std::recursive_mutex mtx; // 全局递归互斥锁
int shared_data = 0;

void increment(int count) {
    if (count <= 0) return;
    mtx.lock(); // 加锁
    ++shared_data;
    increment(count - 1); // 递归调用
    mtx.unlock(); // 解锁
}

int main() {
    std::thread t1(increment, 5);
    std::thread t2(increment, 5);

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

    std::cout << "Shared data: " << shared_data << std::endl;
    return 0;
}

3. 条件变量(Condition Variable)

条件变量用于线程间的等待和通知机制。

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

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

void worker() {
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait(lock, []{ return ready; }); // 等待条件满足
    std::cout << "Worker thread is processing data\n";
}

void trigger() {
    std::lock_guard<std::mutex> lock(mtx);
    ready = true;
    cv.notify_one(); // 通知一个等待的线程
}

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

    std::this_thread::sleep_for(std::chrono::seconds(1));
    trigger();

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

    return 0;
}

4. 读写锁(Read-Write Lock)

读写锁允许多个线程同时读取共享资源,但只允许一个线程写入共享资源。

#include <iostream>
#include <thread>
#include <shared_mutex>

std::shared_mutex rw_mtx; // 全局读写锁
int shared_data = 0;

void reader(int id) {
    std::shared_lock<std::shared_mutex> lock(rw_mtx); // 读锁
    std::cout << "Reader " << id << " reads data: " << shared_data << std::endl;
}

void writer(int id) {
    std::unique_lock<std::shared_mutex> lock(rw_mtx); // 写锁
    ++shared_data;
    std::cout << "Writer " << id << " writes data: " << shared_data << std::endl;
}

int main() {
    std::thread r1(reader, 1);
    std::thread r2(reader, 2);
    std::thread w1(writer, 1);
    std::thread w2(writer, 2);

    r1.join();
    r2.join();
    w1.join();
    w2.join();

    return 0;
}

5. 原子操作(Atomic Operations)

原子操作可以在不使用锁的情况下实现线程安全的操作。

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

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

void increment() {
    ++shared_data; // 原子操作
}

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

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

    std::cout << "Shared data: " << shared_data << std::endl;
    return 0;
}

总结

选择合适的同步机制取决于具体的应用场景和需求。互斥锁是最基本的同步机制,条件变量用于线程间的等待和通知,读写锁适用于读多写少的场景,原子操作则提供了无锁的线程安全操作。在实际编程中,应根据具体情况选择最合适的同步机制。

0