Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] "almost works" clarification
From: Bjorn Reese (breese_at_[hidden])
Date: 2013-05-22 06:21:47

On 05/21/2013 11:58 PM, Michael Powell wrote:

> I ran across this article, which although a bit dated, is a pretty
> good sketch of what I would consider a fairly robust thread runner
> abstraction, which if you read past the frustration, makes good sense,
> at least in my opinion:

The worker in this blog has a synchronization error. If stop() is called
before run() -- this could happen due to thread scheduling -- then it
will not stop.

If you can live without the notify_all() in stop(), then a better
solution is to pass a cancellation callback as an argument to run(),
and check this instead of checking m_running. Something like this:

   class cancellation
     // Use atomic<bool>
     virtual bool is_cancelled() const = 0;

   class worker
      void run(cancellation& state)
        while (!state.is_cancelled())

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at