溫馨提示×

Linux C++中如何處理多線程同步問題

小樊
37
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;
}

總結

選擇合適的同步機制取決于具體的應用場景和需求?;コ怄i是最基本的同步機制,條件變量用于線程間的等待和通知,讀寫鎖適用于讀多寫少的場景,原子操作則提供了無鎖的線程安全操作。在實際編程中,應根據具體情況選擇最合適的同步機制。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女