溫馨提示×

C++ Linux系統如何進行并發控制

小樊
45
2025-05-10 09:08:02
欄目: 編程語言

在Linux系統中使用C++進行并發控制,通常會涉及到多線程編程。C++11引入了標準庫中的線程支持,使得在C++中進行并發編程變得更加容易和安全。以下是一些基本的并發控制概念和方法:

  1. 線程(std::thread): C++11提供了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::lock_guard, std::unique_lock): std::lock_guardstd::unique_lock是RAII風格的鎖管理類,它們在構造時自動加鎖,在析構時自動解鎖,這樣可以避免忘記解鎖導致的死鎖問題。

    #include <iostream>
    #include <thread>
    #include <mutex>
    
    std::mutex mtx;
    
    void printMessage(const std::string& msg) {
        std::lock_guard<std::mutex> lock(mtx); // 自動管理鎖
        std::cout << msg << std::endl;
    }
    
    int main() {
        // ... 同上
    }
    
  4. 條件變量(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> lock(mtx);
        cv.wait(lock, []{ return ready; }); // 等待條件變量
        std::cout << "Thread " << id << std::endl;
    }
    
    void go() {
        std::lock_guard<std::mutex> lock(mtx);
        ready = true;
        cv.notify_all(); // 通知所有等待的線程
    }
    
    int main() {
        std::thread threads[10];
        // 創建多個線程
        for (int i = 0; i < 10; ++i) {
            threads[i] = std::thread(printId, i);
        }
        go(); // 改變條件并通知線程
        for (auto& th : threads) {
            th.join();
        }
        return 0;
    }
    
  5. 原子操作(std::atomic): 對于簡單的數據類型,可以使用std::atomic來保證操作的原子性,避免使用鎖。

    #include <iostream>
    #include <thread>
    #include <atomic>
    
    std::atomic<int> counter(0);
    
    void incrementCounter() {
        ++counter; // 原子操作
    }
    
    int main() {
        std::thread t1(incrementCounter);
        std::thread t2(incrementCounter);
    
        t1.join();
        t2.join();
    
        std::cout << "Counter: " << counter << std::endl;
        return 0;
    }
    
  6. 信號量(semaphore): Linux系統提供了POSIX信號量(sem_t),可以用于線程同步。

    #include <iostream>
    #include <thread>
    #include <semaphore.h>
    
    sem_t sem;
    
    void printMessage(const std::string& msg) {
        sem_wait(&sem); // 等待信號量
        std::cout << msg << std::endl;
        sem_post(&sem); // 發送信號量
    }
    
    int main() {
        sem_init(&sem, 0, 1); // 初始化信號量
    
        std::thread t1(printMessage, "Hello from thread 1");
        std::thread t2(printMessage, "Hello from thread 2");
    
        t1.join();
        t2.join();
    
        sem_destroy(&sem); // 銷毀信號量
        return 0;
    }
    

在使用這些并發控制機制時,需要注意避免死鎖、競態條件和其他并發相關的問題。合理地設計程序結構和同步策略是編寫高效且安全的多線程程序的關鍵。

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