I have a question about making a thread lock method ... I'm not talking about pthread
lock or any other lock ... I'm talking about atomic
operations and finally, futex
(on Linux (we are talking about Linux only)) ...
I know there is a function named futex
and it takes a UINT32 memory address and performs wait and wake without and its waiting is without involving CPU ... In fact, I know there is a different between a loop + atomic operations and atoimc, then futex ... the first one involving CPU (CPU usage) and the second one doesn't ... I thought about futex
and I know there is an instruction in Assembly named nop
which has no CPU involving, but it comes with 1 latency so in my mind I thought maybe the futex
is something like about a loop with for example, 2000
nop
operations and at the end, it just jumps back to the top and checks if our condition is right ... and if it doesn't, runs that 2000 nop
operations again and this continues, until the condition become right ... Am I right about futex
? Or if I'm wrong about how futex
works, is it possible to create a thread-lock function with this behaviour ? Will be a good thing ?
UPDATE
I created a Linux executable ... a loop with 30000 nop
inside it and a condition check at the top of the loop ... but when i execute the program, my CPU usage went to 99% !!!