Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <deque>
- #include <chrono>
- using namespace std;
- deque<int> pipe;
- mutex globalMutex;
- condition_variable cond;
- void provider()
- {
- int value;
- while (cin >> value)
- {
- {
- lock_guard<mutex> lock(globalMutex);
- pipe.push_back(value);
- }
- cond.notify_all();
- }
- }
- void customer()
- {
- mutex mut;
- unique_lock<mutex> lock(mut);
- while (!pipe.empty() || cin.good()) // Is deque::empty() atomic operation? So is cin.good()?
- {
- cond.wait( lock, [] { return !pipe.empty(); } );
- {
- lock_guard<mutex> lock(globalMutex);
- cout << "You enterred:" << pipe.front() << endl;
- pipe.pop_front();
- }
- this_thread::sleep_for(chrono::seconds(4));
- }
- }
- int main()
- {
- thread t1(provider), t2(customer);
- t1.join();
- t2.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment