Boost logo

Boost :

From: Timmo Stange (ts_at_[hidden])
Date: 2007-03-02 22:51:03


Braddock Gaskill wrote:

>> it isn't ok.
>>
>> boost/function/function_template.hpp lines 653-659
>>
>> template<typename Functor>
>> void assign_to(Functor f)
>> {
>> static vtable_type stored_vtable(f);
>> if (stored_vtable.assign_to(f, functor)) vtable = &stored_vtable;
>> else vtable = 0;
>> }
>>
>> function static is not thread safe
>
> You've just succeeded in scaring the hell out of me.
> Just how unsafe is boost::function?

As far as I can see, the situation here is less problematic than with
the general thread-safety problem of function-local statics. This one
is local to a template member function and the object initialization
is identical for concurrent calls (addresses of two static functions
which only depend on the template arguments), so you cannot end up
with a corrupted stored_vtable. There is however a potential race
condition regarding the internal "this local static variable is
initialized" state. I think with >=2 processors running that same code,
one may see that flag set with the actual object not being properly
initialized from its point of view (due to write reordering by the other
processor).

Given the relative "distance" between this initialization code and a
possible access of either manager or invoker member in the vtable, I
think the problem is of a rather academical nature. But the code is
still not strictly correct from my understanding.

The theoretical outcome is undefined behaviour for the first copy
operation or invocation of each function object with a given
distinct signature and assigned functor type.

Regards

Timmo Stange


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