|
|
|
|
|
by jacquesm
4423 days ago
|
|
Yes, task switches unfortunately do not happen at the level of 'C' code but between machine instructions! Run your compiler -S and save the output, then try to reason through the consequences of a context switch in between every instruction with multiple threads contending for the lock. And have a look at the manual for your CPU to learn about out-of-order execution and cache coherency between multiple cores / cpus. Some interesting reading on the subject: https://www.kernel.org/doc/Documentation/memory-barriers.txt If you want to do this entirely in userspace and you must avoid atomic instructions for some reason consider abstracting out your locking to a separate process and use IPC, first come first serve. |
|