溫馨提示×

C++在Linux系統中如何實現并發控制

小樊
55
2025-04-30 07:01:07
欄目: 編程語言

在Linux系統中,C++可以通過多種方式實現并發控制。以下是一些常用的方法:

1. POSIX Threads (pthreads)

POSIX Threads 是一種標準的線程庫,可以在大多數Unix-like系統上使用,包括Linux。

示例代碼:

#include <pthread.h>
#include <iostream>

void* thread_function(void* arg) {
    std::cout << "Thread is running" << std::endl;
    return nullptr;
}

int main() {
    pthread_t thread;
    int result = pthread_create(&thread, nullptr, thread_function, nullptr);
    if (result != 0) {
        std::cerr << "Error creating thread" << std::endl;
        return 1;
    }
    pthread_join(thread, nullptr);
    std::cout << "Thread finished" << std::endl;
    return 0;
}

2. C++11 標準庫線程

C++11引入了標準庫線程支持,提供了更現代和易用的接口。

示例代碼:

#include <thread>
#include <iostream>

void thread_function() {
    std::cout << "Thread is running" << std::endl;
}

int main() {
    std::thread t(thread_function);
    t.join();
    std::cout << "Thread finished" << std::endl;
    return 0;
}

3. 互斥鎖 (Mutex)

互斥鎖用于保護共享資源,防止多個線程同時訪問。

示例代碼(使用C++11標準庫):

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

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();
    th2.join();

    return 0;
}

4. 條件變量 (Condition Variable)

條件變量用于線程間的同步,允許一個線程等待某個條件成立。

示例代碼(使用C++11標準庫):

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

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;});
    std::cout << "Thread " << id << '\n';
}

void go() {
    std::unique_lock<std::mutex> lck(mtx);
    ready = true;
    cv.notify_all();
}

int main() {
    std::thread threads[10];
    for (int i = 0; i < 10; ++i) {
        threads[i] = std::thread(print_id, i);
    }
    std::cout << "10 threads ready to race...\n";
    go();
    for (auto& th : threads) {
        th.join();
    }
    return 0;
}

5. 信號量 (Semaphore)

信號量是一種更高級的同步機制,可以用于控制對共享資源的訪問。

示例代碼(使用POSIX信號量):

#include <semaphore.h>
#include <pthread.h>
#include <iostream>

sem_t sem;

void* thread_function(void* arg) {
    sem_wait(&sem);
    std::cout << "Thread is running" << std::endl;
    sem_post(&sem);
    return nullptr;
}

int main() {
    sem_init(&sem, 0, 1); // Initialize semaphore with value 1

    pthread_t thread;
    pthread_create(&thread, nullptr, thread_function, nullptr);
    pthread_join(thread, nullptr);

    sem_destroy(&sem);
    return 0;
}

6. 讀寫鎖 (Reader-Writer Lock)

讀寫鎖允許多個讀取者同時訪問共享資源,但寫入者獨占訪問。

示例代碼(使用C++17標準庫):

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

std::shared_mutex rw_mtx;

void read_function() {
    std::shared_lock<std::shared_mutex> lock(rw_mtx);
    std::cout << "Reading data\n";
}

void write_function() {
    std::unique_lock<std::shared_mutex> lock(rw_mtx);
    std::cout << "Writing data\n";
}

int main() {
    std::thread readers[5];
    std::thread writers[2];

    for (int i = 0; i < 5; ++i) {
        readers[i] = std::thread(read_function);
    }
    for (int i = 0; i < 2; ++i) {
        writers[i] = std::thread(write_function);
    }

    for (auto& th : readers) {
        th.join();
    }
    for (auto& th : writers) {
        th.join();
    }

    return 0;
}

這些方法可以根據具體需求選擇使用,以實現高效的并發控制。

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