main:
shared count := 0
shared mutex := semaphore(1)
shared turnstile1 := semaphore(0)
shared turnstile2 := semaphore(1)
shared thread_count := read_integer()
create_thread(secondary, thread_count)
secondary:
while true do
Sentence A
wait(mutex)
if ++count == thread_count then
wait(turnstile2)
signal(turnstile1)
signal(mutex)
wait(turnstile1)
signal(turnstile1)
Sentence B
wait(mutex)
if --count == 0 then
wait(turnstile1)
signal(turnstile2)
signal(mutex)
wait(turnstile2)
signal(turnstile2)