Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2008-01-30 01:34:32


Kesseli Pascal wrote:

> Hi everyone
>
> Recently I was trying to synchronize one of my functions using the
> following code:
>
> // write own data to shared vector...
> // ...
> if (++numberOfReadyThreads < numberOfInvolvedThreads) {
> allChargingDataReady.wait(lock);
> return;
> }
> numberOfReadyThreads = 0; // Only the last thread enters this section.
> // write data to file...
> allChargingDataReady.singal_all();
>
> The idea is that all threads submit their data using this function and
> then wait before reading in the next set of data until all threads have
> submitted their data. The last thread to submit his data is the one who
> has to write the collected data to a file.
>
> Using this structure, I find it very simple to realize this behavior.
> However, I know from programming with Java that the VM there sometimes
> awakens one's threads without any reason, which means that I would have to
> realize my condition like this instead:
>
>
> // ...
> while(numberOfReadyThreads < numberOfInvolvedThreads) {
> allChargingDataReady.wait(lock);
> }
> // ...
>
> Implementing the same behavior using this snippet is much more complicated
> though and I would rather avoid it if possible.

Why is it more complicated?

> And that leads us right
> away to my question:
>
> Is the while()-workaround using Boost & C++ needed as well as in Java? Or
> can I use the much simpler if()-version?

As docs say, you need to test your condition in a loop in Boost.Threads.
This is not a particular property of Java or Boost.Threads -- that's how
POSIX threads work. (Windows don't have native condition, and I have much
clue how conditions are simulated on windows and whether window Event thing
has similar behaviour).

- Volodya


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