But then, if there’s a way to lock a mutex so that it’s impossible for two threads to do it at the same time, why not use that mechanism to modify whatever shared variable is in question?
I imagine that pthread or if i want to share an integer from my program with another thread, why cant I just use whatever is used by the mutex?
If that is possible to happen, you don't have a correctly implemented mutex.
So no.
> But then, if there’s a way to lock a mutex so that it’s impossible for two threads to do it at the same time, why not use that mechanism to modify whatever shared variable is in question?
Because it's fiddly and hardware-dependent. Do you want to rewrite your concurrency code, just because you moved your program to a slightly different processor?
Also, adon't you need other data types than a bit (or a small integer)? The hardware-supported exclusion mechanism certainly won't know about strings or your ApplicationFactory class.
If your structure is larger than your architecture can do atomic compare and swap, then you'll need something else.
Mutexes usually give you a mechanism to wait for held locks as well. Often without spinning, sometimes with some degree of fairness.