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
30
31
32
33
34
35 | function main(argc, argv[]):
if argc = 4 then
shared team_count := integer(argv[1])
shared stage1_delay := integer(argv[1])
shared stage2_delay := integer(argv[1])
shared start_barrier := barrier(team_count)
shared batons := semaphore(0)[team_count]
shared position := 0
shared can_access_position := mutex()
for team := 0 to team_count do
create_thread(run_stage1, team)
create_thread(run_stage2, team)
end for
else
print "usage: relay_race team_count stage1_delay stage2_delay"
end if
end function
function run_stage1(team_number):
wait(start_barrier)
delay(stage1_delay)
signal(batons[team_number])
end function
function run_stage2(team_number):
wait(batons[team_number])
delay(stage2_delay)
lock(can_access_position)
my_team_position := ++position
if my_team_position <= 3 then
print('Place ', my_team_position, ': team ', team_number)
end if
unlock(can_access_position)
end function
|