Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2007-12-19 17:25:20


Douglas Gregor wrote:
> Phil Endecott wrote:
>> - What exactly is the race between? E.g. if I know that I'll never
>> create two threads at the same time, do I still need to worry about
>> other uses of Boost.Function that may be concurrent? Is the race
>> between creating the function objects or between using them, or both?
>
> The race will occur if you construct or assign to two boost::function
> objects:
> - that have the same type, and
> - with target function objects of the same type, and
> - you have never completely constructed/assigned to a
> boost::function object of that type with that target function object type

OK, so the common case where I have a main thread that spawns
per-operation threads:

void handle_request(request_t req) { .... }
void main() {
   while (1) {
     request_t r = receive_request();
     thread t(bind(handle_request,r));
   }
}

is safe, if this is the only place where threads are created. Another
common case in my code is a per-object thread:

class C {
   void run() { .... }
   thread run_thread;
public:
   C():
     run_thread(bind(C::run,this))
   {}
   ~C() { run_thread.join(); }
};

Because C is in the type, I only need to worry about concurrent
creation of C objects; there's no race with threads in other classes.

>> - Can I avoid the problem by using a new version of Boost.Function,
>> without upgrading other libraries? (Is Boost.Function header-only?)

[No, because]
> You should recompile anything that depends on Function (e.g., the threads
> library)

> This should have gone on the web page, at the very least, and I'll do so
> now.

Thanks.

Phil.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk