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
procedure main()
  shared hydrogen_multiplex := create_semaphore(2)
  shared oxygen_multiplex := create_semaphore(1)
  shared device_barrier := crate_barrier(3)

  while true do
    case read_char() of
      'H': create_thread(hydrogen)
      'O': create_thread(oxygen)
      EOF: return
    end case
  end while
end procedure

procedure hydrogen()
  wait(hydrogen_multiplex)
  wait(device_barrier)
  bond()
  wait(device_barrier)
  signal(hydrogen_multiplex)
end procedure

procedure oxygen()
  wait(oxygen_multiplex)
  wait(device_barrier)
  bond()
  wait(device_barrier)
  signal(oxygen_multiplex)
end procedure