Download pseudo source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
procedure main(argc, argv[])
  shared next_thread := 0
  shared thread_count := integer(argv[1])
  for thread_number := 0 to thread_count do
    create_thread(greet, thread_number) // thread team
  end for
  print "Hello from main thread"
end procedure

procedure greet(thread_number)
  // Wait until it is my turn
  while next_thread < thread_number do
    // busy-wait
  end while

  print "Hello from secondary thread", thread_number, " of ", thread_count

  // Allow subsequent thread to do the task
  next_thread := next_thread + 1
end procedure