You've just rediscovered the fact that shared-state concurrency is a nasty mess. Humans aren't smart enough to use it correctly in anything but the most controlled conditions. Declarative concurrency, on the other hand, is possible to understand. Check into Concepts, Techniques, and Models of Computer Programming. They've also got a wiki