Fix randomly failing scheduler_tests test
Summary:
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.
Test Plan:
ninja all check
Reviewers: #bitcoin_abc, PiRK
Reviewed By: #bitcoin_abc, PiRK
Differential Revision: https://reviews.bitcoinabc.org/D13177