The root cause is that the original test author was misusing threads and/or synchronization primitives. There is no guarantee that the first main thread runs before the last scheduled task. As such, sometimes, the last task runs and writes "42" into the `counter` before the `BOOST_CHECK_EQUAL(counter, 0)` line gets a chance to be evaluated in the main thread.
Port of bchn#1189.