-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
#include<thread>
#include<mutex>
#include<atomic>
#include<deque>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<condition_variable>
#include<iostream>
using namespace std;
std::mutex m;
std::deque<int>q;
std::condition_variable cv;
//consumer
int user()
{
int i=0;
srand(time(NULL));
for(int j=0;j<100;j++)
{
std::unique_lock<std::mutex>lock(m);
i = rand()%100;
if (i % 2 == 0)
{
q.push_back(i);
cv.notify_one();
}
else {
continue;
}
}
return 0;
}
//producer
void task() {
int data = 0;
while(true)
{
std::unique_lock<std::mutex>lock(m);
if (q.empty()) {
cv.wait(lock);
}
if (!q.empty()) {
data = q.front();
q.pop_front();
std::cout << "Get value from que" << data << std::endl;
}
}
}
int main() {
std::thread t1(user);
std::thread t2(task);
t1.join();
t2.join();
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels