C++ 多线程笔记(std::thread)

                     

贡献者: addis

   例程(编译时要给 linker 加上 -l pthread):

#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

mutex mut;

// A dummy function
void myfun(int id, int *i)
{
    if (id == 1)
        this_thread::sleep_for(chrono::milliseconds(100));
    lock_guard<mutex> guard(mut);
    printf("id = %d, i = %d\n", id, *i);
    int j = *i+1;
    if (id == 2)
        this_thread::sleep_for(chrono::milliseconds(100));
    *i = j;
}

int main()
{
    int i = 0;
    thread th1(myfun, 1, &i);
    thread th2(myfun, 2, &i);
    myfun(0, &i);
    th1.join();
    th2.join();
    return 0;
}

   另外也可以把 mut 声明为 myfun() 中的一个 static 变量。

   另外注意 mutex 不光适用于 std::thread,对任何其他 threading 库例如 pthread 或者 OpenMP 都有效。

  

未完成:互斥锁、条件锁、读写锁以及自旋锁分别什么时候用?

thread_local 变量

1. std::condition_variable

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

std::mutex mtx;
std::condition_variable cv_producer;
std::condition_variable cv_consumer;
std::queue<int> buffer;
const unsigned int max_buffer_size = 10;

void producer(int id) {
    for (int i = 0; i < 20; ++i) {
        std::unique_lock<std::mutex> lck(mtx);
        // Wait until buffer is not full
        cv_producer.wait(lck, []{ return buffer.size() < max_buffer_size; });
        buffer.push(i);
        std::cout << "Producer " << id << " produced " << i << "\n";
        cv_consumer.notify_one(); // Notify one consumer
    }
}

void consumer(int id) {
    for (int i = 0; i < 10; ++i) {
        std::unique_lock<std::mutex> lck(mtx);
        // Wait until buffer is not empty
        cv_consumer.wait(lck, []{ return !buffer.empty(); });
        int item = buffer.front();
        buffer.pop();
        std::cout << "Consumer " << id << " consumed " << item << "\n";
        cv_producer.notify_one(); // Notify one producer
    }
}

int main() {
    std::thread producers[2];
    std::thread consumers[4];

    for (int i = 0; i < 2; ++i)
        producers[i] = std::thread(producer, i);

    for (int i = 0; i < 4; ++i)
        consumers[i] = std::thread(consumer, i);

    for (auto& p : producers) p.join();
    for (auto& c : consumers) c.join();

    return 0;
}

2. 自旋锁

   c++ 没有定义自旋锁,我们可以简单实现一个:

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

class SpinLock {
private:
    std::atomic_flag lockFlag = ATOMIC_FLAG_INIT;

public:
    void lock() {
        while (lockFlag.test_and_set(std::memory_order_acquire)) {
            // Spin-wait (busy wait) until the lock is released
        }
    }

    void unlock() {
        lockFlag.clear(std::memory_order_release);
    }
};

void exampleFunction(SpinLock& spinLock) {
    spinLock.lock();
    std::cout << 123 << "234" << std::endl;
    spinLock.unlock();
}

int main() {
    SpinLock spinLock;
    std::thread t1(exampleFunction, std::ref(spinLock));
    std::thread t2(exampleFunction, std::ref(spinLock));

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

    return 0;
}

                     

© 小时科技 保留一切权利