If I set a set_wait_callback on a boost::unique_future, is it guaranteed to only run once?
I'm a bit suspicious since when looking at the source code I find the following:
struct relocker
{
boost::unique_lock<boost::mutex>& lock;
relocker(boost::unique_lock<boost::mutex>& lock_):
lock(lock_)
{
lock.unlock();
}
~relocker()
{
lock.lock();
}
private:
relocker& operator=(relocker const&);
};
void do_callback(boost::unique_lock<boost::mutex>& lock)
{
if(callback && !done)
{
boost::function<void()> local_callback=callback;
relocker relock(lock); // unlock mutex?
local_callback();
}
}
void wait(bool rethrow=true)
{
boost::unique_lock<boost::mutex> lock(mutex);
do_callback(lock);
while(!done)
{
waiters.wait(lock);
}
if(rethrow && exception)
{
boost::rethrow_exception(exception);
}
}
Where in do_callback the mutex is actually unlocked while the callback is invoked, which from my understanding can lead to the callback being called multiple times if multiple threads call the wait function?
Can the callback be called multiple times? Is it by design? Or am I missing something?
The reason I'm a bit surprised is that in the C++11 standard the async(std::launch::deferred, ...) (to which set_wait_callback is a cousin), seems to have single invocation guarantee:
§30.6.8
The shared state is not made ready until the function has completed. The first call to a non-timed waiting function (30.6.4) on an asynchronous return object referring to this shared state shall invoke the deferred function in the thread that called the waiting function.