riccardinofuffolo

PDS20150625

Jun 25th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. #pragma once
  2. #include <mutex>
  3. #include <condition_variable>
  4. template <class T>
  5. class Exchanger {
  6. private:
  7.     T val;
  8.     std::condition_variable cv;
  9.     std::mutex m;
  10. public:
  11.     Exchanger() {
  12.     }
  13.     T exchange(T t) {
  14.         std::unique_lock<std::mutex> ul(m);
  15.         val = t;
  16.         cv.notify_one();
  17.         cv.wait(ul);
  18.         return val;
  19.     }
  20.     ~Exchanger() {
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment