57 lines
1.1 KiB
Plaintext
57 lines
1.1 KiB
Plaintext
#include "ontimer.bi"
|
|
|
|
Randomize Timer
|
|
Dim Shared As Uinteger nWorkers = 3
|
|
Dim Shared As Uinteger tID(nWorkers)
|
|
Dim Shared As Integer cnt(nWorkers)
|
|
Dim Shared As Integer checked = 0
|
|
|
|
Sub checkpoint()
|
|
Dim As Boolean sync
|
|
|
|
If checked = 0 Then sync = False
|
|
checked += 1
|
|
If (sync = False) And (checked = nWorkers) Then
|
|
sync = True
|
|
Color 14 : Print "--Sync Point--"
|
|
checked = 0
|
|
End If
|
|
End Sub
|
|
|
|
Sub task(worker As Uinteger)
|
|
Redim Preserve cnt(nWorkers)
|
|
|
|
Select Case cnt(worker)
|
|
Case 0
|
|
cnt(worker) = Rnd * 3
|
|
Color 15 : Print "Worker " & worker & " starting (" & cnt(worker) & " ticks)"
|
|
Case -1
|
|
Exit Select
|
|
Case Else
|
|
cnt(worker) -= 1
|
|
If cnt(worker) = 0 Then
|
|
Color 7 : Print "Worker "; worker; " ready and waiting"
|
|
cnt(worker) = -1
|
|
checkpoint
|
|
cnt(worker) = 0
|
|
End If
|
|
End Select
|
|
End Sub
|
|
|
|
Sub worker1
|
|
task(1)
|
|
End Sub
|
|
Sub worker2
|
|
task(2)
|
|
End Sub
|
|
Sub worker3
|
|
task(3)
|
|
End Sub
|
|
|
|
Do
|
|
OnTimer(500, @worker1, 1)
|
|
OnTimer(100, @worker2, 1)
|
|
OnTimer(900, @worker3, 1)
|
|
Sleep 1000
|
|
Loop
|