So far I have a nice spinlock that works as intendend:
std::atomic_flag barrier = ATOMIC_FLAG_INIT;
inline void lock( ){
while( barrier
.test_and_set( std::memory_order_acquire ) )
{}
}
However I want to know (indicatively) how many CPU cycles are spent within it (if busy wait is too long probably I'll consider a mutex which at least puts waiting threads to sleep):
inline void lock( int & waitCounter){
while( barrier
.test_and_set( std::memory_order_acquire ) )
waitCounter++;
}
Ofcourse this does not keep in count the lock instruction itself, so by which constant should I increment the waitCounter to get a precise idea of cycles spent in busy wait (I consider instructions will not be pipelined because of memory barrier so the count is pretty precise in theory)?
waitCounter+=2;
waitCounter+=3;
waitCounter+=4; //...