Download pseudo 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
33
34
35
36
37
38
39
40
41
42
43
44
45
procedure main()
  // How many threads have arrived to the barrier
  shared count := 0
  // Protects the increment of the count
  shared can_access_count := create_semaphore(1)
  // Locked (0) until all threads arrive, then it is unlocked (1)
  shared turnstile1 := create_semaphore(0)
  shared turnstile2 := create_semaphore(1)
  // Read thread count from standard input
  input shared const thread_count
  // Create a thread team running secondary
  create_threads(thread_count, secondary)
end procedure

procedure secondary()
  while true do
    Statement A

    // Adapt rendezvous solution here
    wait(can_access_count)
      count := count + 1
      if count = thread_count then
        wait(turnstile2)
        signal(turnstile1)
      end if
    signal(can_access_count)
    wait(turnstile1)
    signal(turnstile1)

    // Statement B can be only executed until all threads have run Statement A
    Statement B

    // Adapt rendezvous solution here
    wait(can_access_count)
      count := count - 1
      if count = 0 then
        wait(turnstile1)
        signal(turnstile2)
      end if
    signal(can_access_count)
    wait(turnstile2)
    signal(turnstile2)

  end while
end procedure