I agree with
NicTheQuick : the statistic changes for not to freeze, but does not cancel the chances to freeze.
When you kill a thread with
killThread(), the info of
IsThread() has a delay.
Code: Select all
; CAREFUL !
; DO NOT EXECUTE THIS CODE !
Procedure thread(*param)
Delay(1000)
EndProcedure
For count = 1 To 1000
thread = CreateThread(@thread(), 0)
If thread
Debug "thread: " + thread
While IsThread(thread)
KillThread(thread)
Delay(1000) ; <------ added
Wend
EndIf
Debug "next"
Next
Normally this code above is okay. But you loose 1000 milliseconds per kill.
So
KillProgram() must be used in the extrem ways.
Prefer this :
Code: Select all
STRUCTURE thr
kill.i
killed.i
endstructure
Procedure thread(*param.thr)
do
Delay(1000)
until *param\kill
*param\killed = 1
EndProcedure
DIM *param.thr(1023)
For count = 1 To 1000
*param(count) = allocatememory(sizeof(integer) )
thread = CreateThread(@thread(), *param(count) )
If thread
Debug "thread: " + thread
;While IsThread(thread)
;KillThread(thread)
;Delay(1000+16) ; <------ added
;Wend
*param(count)\kill = 1
EndIf
Debug "next"
Next
repeat
delay(16)
any = 0
for i = 0 to 1023
if *param(i)\kill
if *param(i)\killed
freememory(*param(i) )
else
any + 1
endif
endif
next
debug str(any) + " thread being killed..."
until any
Debug "All is killed"
"killed" field in "thr" structure is facultative : you could use IsThread() instead of this field. But a thread management is done with a shared memory buffer (pointed here by *param(i) ) and should be composed of two flag fields per action :
Code: Select all
structure thr
doAnything.i ; command
anythingIsDone.i ; result of the command
EndStructure
Whatever the flow (master/slave as slave/master).