Advertisement
SpaceCreator

gnopgnip

Apr 21st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5.  
  6. std::mutex m;
  7. std::mutex cos;
  8. std::mutex sin;
  9.  
  10. void bar(){
  11.     std::unique_lock <std::mutex> lock(m);
  12.     std::unique_lock <std::mutex> lock_sin(sin);
  13.     std::unique_lock <std::mutex> lock_cos(cos, std::defer_lock);
  14.     bool cs = true;
  15.     bool tr = false;
  16.     printf("gnop");
  17.     lock.unlock();
  18.     for(size_t i = 1; i < 500000; ++i) {
  19.         while (!lock.try_lock()) {}
  20.         //printf("i m locked\n");
  21.         tr = lock_cos.try_lock();
  22.         if(tr == cs){
  23.             if(tr){
  24.                 //printf("cos locked\n");
  25.                 lock_cos.unlock();
  26.                 //printf("cos unlocked\n");
  27.             }
  28.             --i;
  29.             //printf("yield\n");
  30.             lock.unlock();
  31.             std::this_thread::yield();
  32.             continue;
  33.         } else if(tr){
  34.             //printf("cos locked\n");
  35.             lock_cos.unlock();
  36.             //printf("cos unlocked\n");
  37.             lock_sin.lock();
  38.             //printf("sin locked\n");
  39.         } else{
  40.             lock_sin.unlock();
  41.             //printf("sin unlocked\n");
  42.         }
  43.         cs = tr;
  44.         printf("gnop\n");
  45.         lock.unlock();
  46.     }
  47. }
  48.  
  49. void foo(){
  50.     std::unique_lock <std::mutex> lock_cos(cos, std::defer_lock);
  51.     std::unique_lock <std::mutex> lock_sin(sin, std::defer_lock);
  52.     std::unique_lock <std::mutex> lock(m,std::defer_lock);
  53.     bool sn = true;
  54.     bool tr = false;
  55.     for(size_t i = 0; i < 500000; ++i) {
  56.         while (!lock.try_lock()) {}
  57.         //printf("o m locked\n");
  58.         tr = lock_sin.try_lock();
  59.         if(tr == sn){
  60.             if(tr){
  61.                 //printf("sin locked\n");
  62.                 lock_sin.unlock();
  63.                 //printf("sin unlocked\n");
  64.             }
  65.             --i;
  66.             //printf("yield\n");
  67.             lock.unlock();
  68.             std::this_thread::yield();
  69.             continue;
  70.         } else if(tr){
  71.             //printf("sin locked\n");
  72.             lock_sin.unlock();
  73.             //printf("sin unlocked\n");
  74.             lock_cos.unlock();
  75.             //printf("cos unlocked\n");
  76.         } else{
  77.             lock_cos.lock();
  78.             //printf("cos locked\n");
  79.         }
  80.         sn = tr;
  81.         printf("gnip\n");
  82.         lock.unlock();
  83.     }
  84. }
  85.  
  86.  
  87.  
  88. int main(int argc, char const *argv[]) {
  89.     std::thread T1 (bar);
  90.     T1.detach();
  91.     std::thread T2 (foo);
  92.     T2.join();
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement