Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <condition_variable>
  3. #include <mutex>
  4. #include <thread>
  5. #include <iostream>
  6. #include <queue>
  7.  
  8. std::queue<int> items_;
  9. std::condition_variable cv;
  10. std::mutex mutex_;
  11.  
  12. void Push(int item) {
  13.   std::unique_lock<std::mutex> lock(mutex_);
  14.  
  15.   items_.push(item);
  16.   if (items_.size() == 1) {
  17.     cv.notify_all();
  18.   }
  19. }
  20.  
  21. void Pop(int& item) {
  22.   std::unique_lock<std::mutex> lock(mutex_);
  23.  
  24.   while (items_.empty()) {
  25.     cv.wait(lock);
  26.   }
  27.   item = items_.front();
  28.   items_.pop();
  29. }
  30.  
  31. void Process() {
  32.   int el;
  33.   std::cin >> el;
  34.   Push(el);
  35.   Pop(el);
  36.   std::cout << el;
  37. }
  38.  
  39. int main() {
  40.   std::thread t1 = std::thread(Process);
  41.   std::thread t2 = std::thread(Push);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement