Download pseudo source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
procedure main:
  shared can_access_count := create_semaphore(1)
  shared count := 0
  shared constant thread_count = read_integer()
  create_threads(thread_count, secondary)
end procedure

procedure secondary:
  // Critical section
  wait(can_access_count)
  count := count + 1
  signal(can_access_count)
end procedure