Download pseudo source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
procedure main(argc, argv[])
  shared thread_count := integer(argv[1])
  shared can_greet as array of thread_count semaphores
  for thread_number := 0 to thread_count do
    can_greet[thread_number] := create_semaphore(not thread_number)
    create_thread(greet, thread_number) // thread team
  end for
  print "Hello from main thread"
end procedure

procedure greet(thread_number)
  wait(can_greet[thread_number])
  print "Hello from secondary thread", thread_number, " of ", thread_count
  signal(can_greet[(thread_number + 1) mod thread_count])
end procedure