Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] race condition detected with valgrind drd
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2010-05-26 02:59:03


Jorge Moraleda <moraleda_at_[hidden]> writes:

> I must be doing something wrong, but I can't figure out what. I would
> appreciate some help. Thank you in advance.
>
> The following program:
>
> #include <boost/thread.hpp>
>
> void threadEntry()
> {
> }
>
> int main (int argc, char *argv[])
> {
> for(size_t t=0; t<10; t++) {
> boost::thread(threadEntry);
> }
> }
>
> runs cleanly with valgrind drd, but this other program produces race
> conditions when run with valgrind drd (see below). The only difference
> is "new".
>
> #include <boost/thread.hpp>
>
> void threadEntry()
> {
> }
>
> int main (int argc, char *argv[])
> {
> for(size_t t=0; t<10; t++) {
> new boost::thread(threadEntry);
> }
> }

I don't know why the result is different. Maybe the result is different
because the compiler optimizes the code differently.

Anyway, it looks like the detected race is in call_once. The code there
relies on the fact that reading and writing integers is atomic, so
you'll never get a part-updated value, even if the value read is
stale. This is technically a race, but works on common platforms.

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net