I have some C code (on Linux) that needs to run in a thread safe manner. It's very low contention but I have to guard it to ensure correctness.
I have the option of using local variables to guard it (which will work based on how the code is written) but since I have a mutex variable, which is the better approach in terms of cpu usage?
As I expect nearly never to have any contention on the lock is acquiring and releasing mutex significantly more costly compared to checking, incrementing and decrementing a local variable ?
I use the mutex elsewhere so by not using it I am not saving any memory.