C++ std::thread 简单的生产者和消费者

来源:互联网 发布:ipad pro app 知乎 编辑:程序博客网 时间:2024/06/06 15:35
#include <iostream>#include <thread>#include <mutex>#include <deque>#include <vector>#include <unistd.h>std::deque<int> data;std::mutex m_mux;//全局互斥锁int num=0;void product_thread(){    while(true){        std::unique_lock<std::mutex> lck(m_mux);        num = ++num % 1000;        data.push_front(num);        printf("product %d\n", num);        lck.unlock();        sleep(2);    }}void consume_thread(){    while (true){        std::unique_lock<std::mutex> lck(m_mux);        if(data.empty()) {            lck.unlock();            continue;        }        printf("consume %d\n", data.back());        data.pop_back();        lck.unlock();        sleep(5);    }}int main() {    std::thread thread_product(&product_thread);    std::cout<<"Now it runs after the thread_product function !"<<std::endl;    std::thread thread (&consume_thread);    std::cout<<"Now it runs after the thread function !"<<std::endl;    thread_product.join();    std::cout<<"Now it runs after the  thread_product function !"<<std::endl;    thread.join();    std::cout<<"Now it runs after the thread.join function !"<<std::endl;    return  0;}

0 0