|
@@ -1,18 +1,28 @@
|
|
| 1 |
// How many threads have arrived to the barrier
|
| 2 |
shared count := 0
|
| 3 |
// Protects the increment of the count
|
| 4 |
shared can_access_count := semaphore(1)
|
| 5 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 6 |
shared barrier := semaphore(0)
|
| 7 |
|
| 8 |
main:
|
| 9 |
thread_count := read_integer()
|
| 10 |
create_threads(thread_count, secondary)
|
| 11 |
|
| 12 |
secondary:
|
| 13 |
Statement A
|
| 14 |
|
| 15 |
// Adapt rendezvous solution here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
// Statement B can be only executed until all threads have run Statement A
|
| 18 |
Statement B
|
|
|
| 1 |
// How many threads have arrived to the barrier
|
| 2 |
shared count := 0
|
| 3 |
// Protects the increment of the count
|
| 4 |
shared can_access_count := semaphore(1)
|
| 5 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 6 |
shared barrier := semaphore(0)
|
| 7 |
|
| 8 |
main:
|
| 9 |
thread_count := read_integer()
|
| 10 |
create_threads(thread_count, secondary)
|
| 11 |
|
| 12 |
secondary:
|
| 13 |
Statement A
|
| 14 |
|
| 15 |
// Adapt rendezvous solution here
|
| 16 |
+
wait(can_access_count)
|
| 17 |
+
count := count + 1
|
| 18 |
+
if count = thread_count then
|
| 19 |
+
for index := 0 to thread_count do
|
| 20 |
+
signal(barrier)
|
| 21 |
+
end for
|
| 22 |
+
end if
|
| 23 |
+
signal(can_access_count)
|
| 24 |
+
|
| 25 |
+
wait(barrier)
|
| 26 |
|
|
|
|
| 27 |
Statement B
|
| 28 |
+
|