1. Suppose thread A is waiting to lock a mutex, and thread B unlocks
it. Is it guaranteed that thread A gets the mutex before thread B
has a chance to do anything else (that is, before it returns from
pthread_mutex_unlock)? In other words, in the following case:
Thread A Thread B
pthread_mutex_lock
pthread_mutex_lock
pthread_mutex_unlock
????? ??????
pthread_mutex_lock
Is it guaranteed that thread A gets the lock before thread B has a
chance to try, and thus thread A holds the mutex at the end? (Ignore
the question of how we know that A was waiting when B unlocked;
suppose that we do know.)
2. If thread A uses pthread_cond_signal to signal condition X, and I
know that thread B is waiting for condition X with mutex Y, is it
guaranteed that thread B has awoken and is waiting to lock mutex Y
(or already has it) when pthread_cond_signal returns? Example
(assume Y starts unlocked):
Thread A Thread B
pthread_cond_wait(X, Y)
pthread_cond_signal(B, Y)
pthread_mutex_lock(Y)
Is it guaranteed that at the end, thread B holds the mutex, and thread
A is waiting?
Also, is there a reference on the topic that explains these cases? Or
is the answer implied in the usual references (I'm using the Linux man
pages and the SuSv2 specs) and I just didn't see it?
Thanks in advance.
--
Nate Eldredge
neld...@hmc.edu
It's guaranteed that A gets the lock when it returns from
pthread_lock(), but it's not guatanteed that it's gonna happen in any
particular moment after B's unlocking the mutex. That is B may or may
not grab the mutex again before A gets a chance.
> 2. If thread A uses pthread_cond_signal to signal condition X, and I
> know that thread B is waiting for condition X with mutex Y, is it
> guaranteed that thread B has awoken and is waiting to lock mutex Y
> (or already has it) when pthread_cond_signal returns? Example
> (assume Y starts unlocked):
>
> Thread A Thread B
> pthread_cond_wait(X, Y)
> pthread_cond_signal(B, Y)
> pthread_mutex_lock(Y)
>
> Is it guaranteed that at the end, thread B holds the mutex, and thread
> A is waiting?
Same thing here - A might get the lock before B has a chance.
> Also, is there a reference on the topic that explains these cases? Or
> is the answer implied in the usual references (I'm using the Linux man
> pages and the SuSv2 specs) and I just didn't see it?
Check out almost any book on POSIX threads. The usually recommended is
http://www.booksamillion.com/ncom/books?pid=0201633922
> Nate Eldredge
Dima
POSIX requires that if threads are blocked on the mutex when it is
unlocked, one of those threads gets the mutex according to the
scheduling policy.
>chance to try, and thus thread A holds the mutex at the end? (Ignore
>the question of how we know that A was waiting when B unlocked;
>suppose that we do know.)
>
>
>2. If thread A uses pthread_cond_signal to signal condition X, and I
> know that thread B is waiting for condition X with mutex Y, is it
> guaranteed that thread B has awoken and is waiting to lock mutex Y
> (or already has it) when pthread_cond_signal returns?
No. The pthread_cond_signal only implies a state change in the waiting
thread(s) from blocked to runnable, nothing more.
Example
> (assume Y starts unlocked):
>
>Thread A Thread B
> pthread_cond_wait(X, Y)
>pthread_cond_signal(B, Y)
>pthread_mutex_lock(Y)
>
>Is it guaranteed that at the end, thread B holds the mutex, and thread
>A is waiting?
No. B can only hold the mutex if A is preempted before it reaches
its pthread_mutex_lock and B is scheduled to run. This could happen by
chance, or if B has a higher real-time priority than A such that
whenever both B and A are runnable, B always gets to run.