From: Michal Schmidt <mschmidt@redhat.com>
To: "Nelson Castillo" <nelsoneci@gmail.com>
Cc: linux-rt-users@vger.kernel.org,
"Andrés Calderón" <andres.calderon@emqbit.com>
Subject: Re: sched_yield() needed for pthread_cond_signal(..) to work?
Date: Tue, 19 Feb 2008 01:35:13 +0100 [thread overview]
Message-ID: <20080219013513.5eb25b97@hammerfall> (raw)
In-Reply-To: <2accc2ff0802181016q26eb8b8cm88f45a0162ae21a0@mail.gmail.com>
Dne Mon, 18 Feb 2008 13:16:16 -0500
"Nelson Castillo" <nelsoneci@gmail.com> napsal(a):
> http://svn.arhuaco.org/svn/src/junk/trunk/threads/cond-signal-test.c
>
> In a PC (Debian 2.6.24 686 with no RT patches) it prints 1000. In the
> realtime system it prints 1 or 0.
>
> # ./a.out
> 1
> # ./a.out
> 0
> # ./a.out
> 1
Your test program is flawed. The result can be anything between 0 and
1000. (Theoretically even more, because spurious wakeups are
possible from pthread_cond_wait).
I ran the program without sched_yield() on my Fedora 8 system
(2.6.23.15-137.fc8 kernel, non-RT, only voluntary preemption). I got
1000 most of the time, but quite often the result was less than that.
> If I do a sched_yield() right after releasing the mutex associated
> with the condition, the program prints 1000 as expected.
You should not rely on that. There's no guarantee it will work. You've
just been lucky so far.
> void
> dosignal (void)
> {
> pthread_mutex_lock (&cond_mutex);
> pthread_cond_signal(&cond);
> pthread_mutex_unlock (&cond_mutex);
> sched_yield(); // without this I get 0! with Linux and RT patches
> }
And you're doing:
for (i = 0; i < 1000; ++i)
dosignal ();
pthread_cond_signal() will wake the other thread up, but you don't know
if it will be scheduled to run immediately. It is perfectly valid for
the scheduler to keep the main thread on the CPU and let it finish all
the 1000 iterations first.
Michal
next prev parent reply other threads:[~2008-02-19 0:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-18 18:16 sched_yield() needed for pthread_cond_signal(..) to work? Nelson Castillo
2008-02-18 18:25 ` Luis Claudio R. Goncalves
2008-02-18 20:24 ` Nelson Castillo
2008-02-19 0:35 ` Michal Schmidt [this message]
[not found] ` <371630906-1203382013-cardhu_decombobulator_blackberry.rim.net-2030994132-@bxe129.bisx.prod.on.blackberry>
2008-02-19 1:51 ` Nelson Castillo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080219013513.5eb25b97@hammerfall \
--to=mschmidt@redhat.com \
--cc=andres.calderon@emqbit.com \
--cc=linux-rt-users@vger.kernel.org \
--cc=nelsoneci@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.