Download c source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)