You are doing it the wrong way - you should acquire the semaphore at the beginning of your operation and release it afterwards. Unfortunately this will not cause both threads to do their work in sequence of A-B-A-B-etc. For that you need two semaphores or wait conditions. Each thread has to wait on one condition (and the other on the other) and when you want the other thread to run, signal the appropriate condition.