sync/{sem_barrier/sem_barrier.alg.c → sem_barrier_reusable/sem_barrier_reusable.alg.c} RENAMED
@@ -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
- // 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
 
 
 
 
 
 
 
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)