pthread_mutex_lock
does not prevent previous instructions to be ordered after it.
Similar, pthread_mutex_unlock
does not prevent followed instructions to be ordered before it.
But:
In threadA global_a = 100
happens-before pthread_mutex_unlock(&b_mutex)
.
In threadB pthread_mutex_lock(&b_mutex)
happens-before int tmp = global_a;
.
And if you observe
pthread_mutex_unlock(&b_mutex)
in threadA happens-before pthread_mutex_lock(&b_mutex)
in threadB.
(in other words, threadB aquires lock after threadA releases it), then
global_a = 100;
in threadA happens-before int tmp = global_a;
in threadB. So, the last one sees effect of the first one.
What POSIX Standard says:
As for synchronisation details in the POSIX standard, the only reference I found (and others refer to) is short chapter about Memory Synchronization. It says that pthread_mutex_lock
(and some other functions)
synchronize memory with respect to other threads
Someones interpret this as full memory barrier garantee, others (and me) prefer to think about some classic garantees, when locking and waiting actions provides memory acquire semantic, unlocking and notifying ones - memory release semantic. See, e.g., this mail.
There is no happens-before term in the POSIX. But it can be defined as usual, taking into account memory order garantees(in one's inerpretation).
pthread_mutex_lock
andpthread_mutex_unlock
? I had google it but nothing. Can you show me some reference about it? – Mohammadmohammed