Audacity 3.2.0
spinlock.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file spinlock.h
6
7 Vitaly Sverchinsky
8
9**********************************************************************/
10
11#pragma once
12
13#include <atomic>
14#include <thread>
15
22{
23 std::atomic_flag flag = ATOMIC_FLAG_INIT;
24public:
26
27 void lock()
28 {
29 for(unsigned i = 0; flag.test_and_set(std::memory_order_acquire); ++i)
30 if(i & 1) std::this_thread::yield();
31 }
32
33 void unlock()
34 {
35 flag.clear(std::memory_order_release);
36 }
37};
Intended for locking of resources that are only lightly contended and locked for very short times,...
Definition: spinlock.h:22
void unlock()
Definition: spinlock.h:33
~spinlock()
Definition: spinlock.h:25
void lock()
Definition: spinlock.h:27
std::atomic_flag flag
Definition: spinlock.h:23