|
@@ -1,13 +1,29 @@
|
|
|
|
|
| 1 |
// How many threads have arrived to the barrier
|
| 2 |
shared count := 0
|
| 3 |
// Protects the increment of the count
|
| 4 |
shared mutex := semaphore(1)
|
| 5 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 6 |
shared barrier := semaphore(0)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
thread_count := read_integer()
|
| 10 |
create_thread(secondary, thread_count)
|
| 11 |
|
| 12 |
secondary:
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 7 |
shared barrier := semaphore(0)
|
| 8 |
|
| 9 |
+
// Create arbitrary amount of threads
|
| 10 |
+
shared thread_count := read_integer()
|
| 11 |
create_thread(secondary, thread_count)
|
| 12 |
|
| 13 |
secondary:
|
| 14 |
+
Statement A
|
| 15 |
+
|
| 16 |
+
// Barrier
|
| 17 |
+
wait(mutex)
|
| 18 |
+
// If this is the last thread that reaches the barrier
|
| 19 |
+
if ++count = thread_count:
|
| 20 |
+
while count > 0:
|
| 21 |
+
--count
|
| 22 |
+
signal(barrier)
|
| 23 |
+
signal(mutex)
|
| 24 |
+
|
| 25 |
+
wait(barrier)
|
| 26 |
+
|
| 27 |
+
// Statement B must be executed until
|
| 28 |
+
// all threads have executed Statement A
|
| 29 |
+
Statement B
|