Code: Select all
dim shared as integer threadStatus(0 to 2)
sub mythread(param as any ptr)
'warning pointer param does not point to a valid memory adress!
dim as integer userData = cast(integer, param) 'unpack
dim as integer threadId = userData and &h0f 'last nibble = id
dim as integer delay = (userData shr 4) \ 100
'start work and update status
for i as integer = 1 to 100
sleep delay, 1 'sleeping is hard work
threadStatus(threadId) = i
next
end sub
'launch threads. sleep time & threadId in packed 1 value
dim as any ptr thread1 = threadCreate(@mythread, cptr(any ptr, (2000 shl 4) + 0))
dim as any ptr thread2 = threadCreate(@mythread, cptr(any ptr, (5000 shl 4) + 1))
dim as any ptr thread3 = threadCreate(@mythread, cptr(any ptr, (8000 shl 4) + 2))
'poll thread status and wait for completion
dim as boolean allDone
do
allDone = true
locate 1,1
for i as integer = 0 to 2
print "thread(" & i & "): " & threadStatus(i) & "%"
if threadStatus(i) < 100 then allDone = false
next
sleep 100, 1
loop until allDone
print "extra wait"
threadWait(thread1) 'or threadDetach
threadWait(thread2)
threadWait(thread3)
print "end!"
sleep 1000