Boost logo

Boost Users :

Subject: Re: [Boost-users] Tracking down a race condition
From: Roland Bock (rbock_at_[hidden])
Date: 2010-01-25 13:38:35


Stephen Torri wrote:
> In the logging example I have been working on to learn more about boost
> threads I have run across what I think is a race condition. If I compile
> and run this sample program it will sometimes write out an error message
> to a file. For example:
>
> 2010-01-23_16:2:23 (pid=10948 thread_id=0x92ce448) (II) funky
>
> Other times I see the file is blank. So can I say conclusively that its
> a race condition? No. Either what is happening is a race condition where
> the file is closed before the contents are written to the file or
> something else is causing the logging message to be written sometimes
> and not at others.
>
>

You can see what is happening by making the writing of logs slower:

    void Trace::threadMain()
    {
       boost::this_thread::sleep(boost::posix_time::seconds(1)); //
Insert this
      boost::unique_lock<boost::mutex> lock ( m_lock );
...

I'd say you need to join the threads before closing the file...

HTH

Roland


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