SHARE
TWEET

Untitled

a guest Jul 19th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <mutex>
  2. #include <condition_variable>
  3. #include <thread>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <vector>
  7. #include <random>
  8. #include <queue>
  9.  
  10. template <typename T>
  11. class Exchanger {
  12.     inline static Exchanger* instance;
  13.     inline static std::once_flag inited;
  14.     std::mutex mutex;
  15.     std::condition_variable to_exchange;
  16.     T* p1 = nullptr;
  17.     T* p2 = nullptr;
  18.     bool ready = false;
  19.  
  20.     Exchanger(){};
  21.     ~Exchanger(){};
  22.  
  23. public:
  24.     static Exchanger* getInstance() {
  25.         std::call_once(inited, []{
  26.             instance = new Exchanger();
  27.         });
  28.         return instance;
  29.     }
  30.  
  31.     T exchange(T t) {
  32.         std::unique_lock lock(mutex);
  33.         if (!ready) {
  34.             p1 = &t;
  35.             ready = true;
  36.             to_exchange.wait(lock, [this]{return !ready;});
  37.             return *p2;
  38.         } else {
  39.             p2 = &t;
  40.             ready = false;
  41.             to_exchange.notify_one();
  42.             return *p1;
  43.         }
  44.  
  45.     }
  46.  
  47. };
  48.  
  49. int* p = nullptr;
  50.  
  51. int exchange(int t) {
  52.     p = &t;
  53. }
  54.  
  55. int main(int argc, char **argv) {
  56.  
  57.     int x = 10;
  58.     exchange(x);
  59.     std::cout << *p << std::endl;
  60.  
  61.     std::vector<std::thread> traders;
  62.     for (int i = 0; i < 4; i++) {
  63.         traders.emplace_back([i]{
  64.             std::this_thread::sleep_for(std::chrono::seconds(rand() % 2));
  65.             std::stringstream msg1;
  66.             msg1 << "thread " << std::this_thread::get_id() << " willing to trade " << i << std::endl;
  67.             std::cout << msg1.str();
  68.  
  69.             std::stringstream msg2;
  70.             msg2 << "thread " << std::this_thread::get_id() << " got " << Exchanger<int>::getInstance()->exchange(i) << std::endl;
  71.             std::cout << msg2.str();
  72.         });
  73.     }
  74.  
  75.     for (int i = 4; i < 8; i++) {
  76.         traders.emplace_back([i]{
  77.             std::this_thread::sleep_for(std::chrono::seconds(rand() % 2));
  78.             std::stringstream msg1;
  79.             msg1 << "thread " << std::this_thread::get_id() << " willing to trade " << i << std::endl;
  80.             std::cout << msg1.str();
  81.  
  82.             std::stringstream msg2;
  83.             msg2 << "thread " << std::this_thread::get_id() << " got " << Exchanger<int>::getInstance()->exchange(i) << std::endl;
  84.             std::cout << msg2.str();
  85.         });
  86.     }
  87.  
  88.     for (auto &t: traders) {
  89.         if (t.joinable()) {
  90.             t.join();
  91.         }
  92.     }
  93.  
  94.     return 0;
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top