#include <iostream>
#include <thread>
#include <condition_variable>
#include <queue>
#include <mutex>
std::queue<int> g_queue;
std::condition_variable g_cv;
std::mutex mtx;
void Producer()
{
for(int i=0;i<100;i++)
{
{
std::unique_lock<std::mutex> lock(mtx);
g_queue.push(i);
// 通知消费者
g_cv.notify_one();
std::cout << "task... value : "<< i << std::endl;
}
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
void Consumer(){
while (1) {
std::unique_lock<std::mutex> lock(mtx);
g_cv.wait(lock,[](){
return !g_queue.empty();
});
int value = g_queue.front();
g_queue.pop();
std::cout << "消费者:" << value << std::endl;
}
}
int main() {
std::thread t1(Producer);
std::thread t2(Consumer);
t1.join();
t2.join();
std::cout << "Hello, World! heool suisha" << std::endl;
return 0;
}