Programming with threads has always been a tricky proposition. Reasoning about multiple threads of execution, and properly implementing locking strategies is difficult to do properly and requires great care and attention to detail. Unfortunately, it may not even be possible, as Hans-J. Boehm shows in his paper, Threads Cannot Be Implemented as a Library. He demonstrates that in the presence of optimizing compilers, you can’t know the sequence of operations really being executed in your code, and therefore, you can’t correctly call thread primitives in libraries. Only threads implemented as part of the language can be correct in all cases.
This stuff is fascinating, but it hurts my brain!