Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two questions

0 views
Skip to first unread message

Nate Eldredge

unread,
Mar 3, 2001, 2:12:28 AM3/3/01
to
I'm just getting started playing with pthreads, and there are a few
questions I have to which I can't find answers in the manuals.

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

Dima Volodin

unread,
Mar 3, 2001, 8:27:00 AM3/3/01
to
Nate Eldredge wrote:
>
> I'm just getting started playing with pthreads, and there are a few
> questions I have to which I can't find answers in the manuals.
>
> 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.)

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

Kaz Kylheku

unread,
Mar 3, 2001, 11:19:59 AM3/3/01
to
On 02 Mar 2001 23:12:28 -0800, Nate Eldredge <neld...@hmc.edu> wrote:
>I'm just getting started playing with pthreads, and there are a few
>questions I have to which I can't find answers in the manuals.
>
>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

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.

0 new messages