|
@@ -1,29 +1,32 @@
|
|
| 1 |
main:
|
| 2 |
-
// How many threads have arrived to the barrier
|
| 3 |
shared count := 0
|
| 4 |
-
// Protects the increment of the count
|
| 5 |
shared mutex := semaphore(1)
|
| 6 |
-
|
| 7 |
-
shared
|
| 8 |
|
| 9 |
-
// Create arbitrary amount of threads
|
| 10 |
shared thread_count := read_integer()
|
| 11 |
create_thread(secondary, thread_count)
|
| 12 |
|
| 13 |
secondary:
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
// Barrier
|
| 17 |
wait(mutex)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
--count
|
| 22 |
-
signal(barrier)
|
| 23 |
signal(mutex)
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
main:
|
|
|
|
| 2 |
shared count := 0
|
|
|
|
| 3 |
shared mutex := semaphore(1)
|
| 4 |
+
shared turnstile1 := semaphore(0)
|
| 5 |
+
shared turnstile2 := semaphore(1)
|
| 6 |
|
|
|
|
| 7 |
shared thread_count := read_integer()
|
| 8 |
create_thread(secondary, thread_count)
|
| 9 |
|
| 10 |
secondary:
|
| 11 |
+
while true do
|
| 12 |
+
Sentence A
|
| 13 |
|
|
|
|
| 14 |
wait(mutex)
|
| 15 |
+
if ++count == thread_count then
|
| 16 |
+
wait(turnstile2)
|
| 17 |
+
signal(turnstile1)
|
|
|
|
|
|
|
| 18 |
signal(mutex)
|
| 19 |
|
| 20 |
+
wait(turnstile1)
|
| 21 |
+
signal(turnstile1)
|
| 22 |
|
| 23 |
+
Sentence B
|
| 24 |
+
|
| 25 |
+
wait(mutex)
|
| 26 |
+
if --count == 0 then
|
| 27 |
+
wait(turnstile1)
|
| 28 |
+
signal(turnstile2)
|
| 29 |
+
signal(mutex)
|
| 30 |
+
|
| 31 |
+
wait(turnstile2)
|
| 32 |
+
signal(turnstile2)
|