codegram/futuroscope — pull request #14 by ggPeti (2014-01-31 14:20:48+00:00)
Title: Fixed race condition with 2 threads waiting on the same future

When 2 threads try to access the same future that has not been resolved yet, both threads start to pop the future's internal queue. But since only one value will ever get pushed to that queue, one of the threads will hang forever.

The solution is to use a mutex, preferably with [double checked locking](http://en.wikipedia.org/wiki/Double-checked_locking) on `resolved_future_value`. This way only one thread will ever block the Future's internal queue, the rest blocks on the mutex and will correctly obtain the value afterwards.

This behavior might have sparked the following line in the original blog post:
> You could reach dead locks when you're calling futures inside of futures (because of the thread pool). You shouldn't be doing that.

Currently I see no other way of reaching a deadlock, so this might be obsolete information after the fix, but I haven't proven this.