void unlock() {
     tid zero(0);
     tid thread_id = this_thread::get_id();
     if(!block.compare_exchange_strong(thread_id, zero, memory_order_release)) {
         throw new exception;
     }
 }
Exemplo n.º 2
0
 void unlock(int threadId) {
     int freeFlag = -1, toReplace = threadId;;
     if (!owner.compare_exchange_strong(threadId, freeFlag)) {
         cerr << "Current owner " << threadId << " but "
         << "Thread number " + to_string(toReplace)
         << " is not the owner!" << endl;
     }
 }
 void lock() {
     tid zero(0);
     tid thread_id = this_thread::get_id();
     while(!block.compare_exchange_strong(zero, thread_id, std::memory_order_acquire)) {
         std::this_thread::yield();
         zero = tid(0);
     }
 }
int foo(atomic<int>& x)
{
  for(size_t n = 0; ; ++n)
  {
    auto expected = x.load();
    auto desired = 0;
    x.compare_exchange_strong(
      expected,
      desired);

    if(n == loop)
      return desired;
  }
}
Exemplo n.º 5
0
void IncrementSharedValue10000000Times(RandomDelay& randomDelay)
{
    int count = 0;
    while (count < 10000000)
    {
        randomDelay.doBusyWork();
        int expected = 0;
        if (flag.compare_exchange_strong(expected, 1, memory_order_relaxed))
        {
            // Lock was successful
            sharedValue++;
            flag.store(0, memory_order_relaxed);
            count++;
        }
    }
}
Exemplo n.º 6
0
 void lock(int threadId) {
     int expected = -1;
     while (!owner.compare_exchange_strong(expected, threadId)) {
         expected = -1;
     }
 }
 bool try_lock() {
     tid zero(0);
     tid thread_id = this_thread::get_id();
     if(!block.compare_exchange_strong(zero, thread_id)) return false;
     else return true;
 }