#include #include #include #include #include // for user-defined resource sections struct SpinLock { void lock() { termsec::CheckFunction check; while ( _flag.test_and_set() ) { } } void unlock() { _flag.clear(); } private: std::atomic_flag _flag = ATOMIC_FLAG_INIT; }; int main() { pthread_rwlock_t rwl = PTHREAD_RWLOCK_INITIALIZER; std::mutex mtx; SpinLock spin; std::thread t0( [&] { while ( true ) { std::unique_lock lmtx( mtx ); // wait for: pthread_rwlock_wrlock( &rwl ); pthread_rwlock_unlock( &rwl ); } } ), t1( [&] { while ( true ) { pthread_rwlock_rdlock( &rwl ); // wait for: { std::unique_lock lsp( spin ); } pthread_rwlock_unlock( &rwl ); } } ), t2( [&] { while ( true ) { std::unique_lock lsp( spin ); // wait for: std::unique_lock lmtx( mtx ); } } ); int x = 0; while ( true ) { x = (x + 1) % 2; } }