Download c source code

shared hydrogen_multiplex := semaphore(2)
shared oxygen_multiplex := semaphore(1)
shared enter_barrier := barrier(3)
shared exit_barrier := barrier(3)

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

hydrogen:
  wait(hydrogen_multiplex);
  wait(enter_barrier);
  bond()
  wait(exit_barrier);
  signal(hydrogen_multiplex);

oxygen:
  wait(oxygen_multiplex);
  wait(enter_barrier);
  bond()
  wait(exit_barrier);
  signal(oxygen_multiplex);