C++系列十:日常学习-多线程

发布时间 2023-09-06 10:52:26作者: cactus9

TOC

介绍:

C++ 是一种支持多线程编程的编程语言,它提供了丰富的多线程支持来充分利用现代多核处理器的性能。
C++ 多线程编程通常使用标准库中的 头文件以及其他相关的标准库组件来实现。

理论:

  1. 常用的类:
    stdthread 类,用于创建和管理线程等等
    std
    this_thread 命名空间中的函数来处理线程的等待和分离等等
    互斥锁(stdmutex)、条件变量(stdcondition_variable)和原子操作(std::atomic)

  2. 线程池:
    线程池是一种管理和复用线程的技术,以避免频繁创建和销毁线程。C++ 标准库中没有直接提供线程池的实现,但你可以使用第三方库或自己编写一个简单的线程池。

  3. 请注意,多线程编程可能会引入一些复杂性和潜在的问题,如竞态条件和死锁。因此,确保充分理解多线程编程的概念和最佳实践,并使用适当的同步机制来确保线程安全是非常重要的。

案例:

std::thread:
join() //阻塞当前线程,直到目标线程执行完毕。
detach() //将线程分离,使其成为后台线程,不再受到 join() 的控制。


//创建互斥锁,并在访问共享资源之前进行锁定。
std::mutex mtx;
mtx.lock();   // 锁定互斥锁
// 访问共享资源
mtx.unlock(); // 解锁互斥锁


//自动管理锁的生命周期,以确保在离开作用域时自动释放锁。
std::mutex mtx;
{
    std::lock_guard<std::mutex> lock(mtx); // 自动锁定和解锁
    // 访问共享资源
} // 离开作用域时自动解锁


//条件变量用于在线程之间进行通信和同步。它们允许一个线程等待另一个线程发出的通知,以执行某些操作。
std::condition_variable cv;
// 线程1等待通知
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock); // 阻塞线程1,直到收到通知
// 线程2发送通知
cv.notify_one(); // 通知线程1


//原子操作:
std::atomic<int> counter(0);
counter.fetch_add(1, std::memory_order_relaxed); // 原子递增操作


//案例1:
#include <iostream>
#include <thread>

void myFunction() {
    // 线程1执行的代码
}
void myFunction1(int value) {
    // 线程2执行的代码
}

int main() {
    std::thread t1(myFunction); // 创建新线程并启动
    std::thread t2(myFunction1,100); // 创建新线程并启动
    t1.join(); // 等待线程完成
    t2.join(); // 等待线程完成
    return 0;
}

//案例2:
#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void myFunction(int& counter) {
    for (int i = 0; i < 1000; ++i) {
        std::lock_guard<std::mutex> lock(mtx); // 使用互斥锁保护共享资源
        counter++;
    }
}

int main() {
    int counter = 0;
    std::thread t1(myFunction, std::ref(counter));
    std::thread t2(myFunction, std::ref(counter));
    t1.join();
    t2.join();
    std::cout << "Counter: " << counter << std::endl;//Counter: 2000
    return 0;
}


生产者-消费者问题:

这就有意思多了,多看多吸收,多悟
不理解得就多问问Chatjpt。

//一对一:
#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>

std::queue<int> buffer;
std::mutex mtx;
std::condition_variable cv;

void producer() {
    for (int i = 0; i < 10; ++i) {
        std::this_thread::sleep_for(std::chrono::milliseconds(200));

        // 锁定互斥锁以保护共享资源
        {
            std::lock_guard<std::mutex> lock(mtx);
            buffer.push(i);
            std::cout << "Produced: " << i << std::endl;
        }

        // 通知消费者线程有新数据可用
        cv.notify_one();
    }
}

void consumer() {
    for (int i = 0; i < 10; ++i) {
        // 锁定互斥锁,等待条件变量通知
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, []{ return !buffer.empty(); });

        // 消费数据
        int data = buffer.front();
        buffer.pop();

        // 解锁互斥锁
        lock.unlock();

        std::cout << "Consumed: " << data << std::endl;
    }
}

int main() {
    std::thread producerThread(producer);
    std::thread consumerThread(consumer);

    producerThread.join();
    consumerThread.join();

    return 0;
}

//一对多:
#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <vector>

std::queue<int> buffer;
std::mutex mtx;
std::condition_variable cv;
const int numProducers = 3; // 多个生产者线程

void producer(int id) {
    for (int i = 0; i < 5; ++i) {
        std::this_thread::sleep_for(std::chrono::milliseconds(200));

        {
            std::lock_guard<std::mutex> lock(mtx);
            int data = id * 100 + i;
            buffer.push(data);
            std::cout << "Producer " << id << " produced: " << data << std::endl;
        }

        cv.notify_one();
    }
}

void consumer() {
    for (int i = 0; i < 15; ++i) { // 总共消费 15 个数据项
        std::unique_lock<std::mutex> lock(mtx);

        cv.wait(lock, []{ return !buffer.empty(); }); // 等待缓冲区非空

        int data = buffer.front();
        buffer.pop();

        lock.unlock();

        std::cout << "Consumer consumed: " << data << std::endl;
    }
}

int main() {
    std::vector<std::thread> producerThreads;

    for (int i = 0; i < numProducers; ++i) {
        producerThreads.push_back(std::thread(producer, i));
    }

    std::thread consumerThread(consumer);

    for (std::thread& thread : producerThreads) {
        thread.join();
    }
    consumerThread.join();

    return 0;
}


//多对多
#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <vector>

std::queue<int> buffer; // 缓冲区队列
std::mutex mtx; // 互斥锁,用于保护共享资源
std::condition_variable cv; // 条件变量,用于线程间通信

const int numProducers = 2; // 多个生产者线程
const int numConsumers = 2; // 多个消费者线程
const int bufferSize = 10; // 缓冲区大小

void producer(int id) {
    for (int i = 0; i < 5; ++i) { // 生产者每个生成 5 个数据项
        std::this_thread::sleep_for(std::chrono::milliseconds(200));

        {
            std::unique_lock<std::mutex> lock(mtx);

            // 等待缓冲区有空间可用
            cv.wait(lock, []{ return buffer.size() < bufferSize; });

            int data = id * 100 + i;
            buffer.push(data);
            std::cout << "Producer " << id << " produced: " << data << std::endl;
        }

        cv.notify_all(); // 通知所有等待的线程
    }
}

void consumer(int id) {
    for (int i = 0; i < 5; ++i) { // 消费者每个消费 5 个数据项
        std::this_thread::sleep_for(std::chrono::milliseconds(200));

        {
            std::unique_lock<std::mutex> lock(mtx);

            // 等待缓冲区非空
            cv.wait(lock, []{ return !buffer.empty(); });

            int data = buffer.front();
            buffer.pop();

            lock.unlock(); // 解锁互斥锁,允许其他线程进入临界区

            std::cout << "Consumer " << id << " consumed: " << data << std::endl;
        }

        cv.notify_all(); // 通知所有等待的线程
    }
}

int main() {
    std::vector<std::thread> producerThreads;
    std::vector<std::thread> consumerThreads;

    for (int i = 0; i < numProducers; ++i) {
        // 创建生产者线程,并传递唯一的标识符 i
        producerThreads.push_back(std::thread(producer, i));
    }

    for (int i = 0; i < numConsumers; ++i) {
        // 创建消费者线程,并传递唯一的标识符 i
        consumerThreads.push_back(std::thread(consumer, i));
    }

    for (std::thread& thread : producerThreads) {
        thread.join(); // 等待生产者线程结束
    }

    for (std::thread& thread : consumerThreads) {
        thread.join(); // 等待消费者线程结束
    }

    return 0;
}