Page 1 of 1
KillThread kills program too
Posted: Thu Nov 21, 2024 4:53 pm
by Sergey
I run code many times and sometimes it freezes program and I write test code to check this moment
Code: Select all
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)
Wend
EndIf
Debug "next"
Next
Tested on Win10 x64 PB6.12
While ... Wend maybe replaced with If ... EndIf nothing will change
// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)
Re: KillThread kills program too
Posted: Thu Nov 21, 2024 5:59 pm
by Axolotl
I don't know what you want to achieve.
I would not use IsThread() in this case, but instead WaitThread() see below.
Code: Select all
Procedure thread(*param)
Delay(1000)
EndProcedure
timeout = 1100 ; start
For count = 1 To 10 ;00
thread = CreateThread(@thread(), 0)
If thread
Debug "thread: " + thread
; While IsThread(thread)
If WaitThread(thread, timeout / count) = 0
Debug " timeout " + Str(timeout / count) + " -> kill the thread: " + thread
KillThread(thread)
EndIf
EndIf
Debug "next"
Next
Re: KillThread kills program too
Posted: Thu Nov 21, 2024 7:57 pm
by jacdelad
Firstly, the while loop has a chance of trying to kill it multiple times. But you already pointed this out.
Now, as the help states, you shouldn't use KillThread, but instead set or global variable or something, which tells the thread to stop.
Also, your program is far from reality and I don't know what you want to achieve.
And finally: This is surely not a bug. Don't post in the bug section before it's a confirmed bug. Mods can move your thread from coding questions if the bug is confirmed.
Re: KillThread kills program too
Posted: Thu Nov 21, 2024 11:15 pm
by Sergey
And thank everyone again for the testing
Axolotl, nice code and WaitThread works perfectly, but not KillThread without waiting
jacdelad, yes I make global var and not have a freeze, thank for advice about a bug for the future
Why there's a standard procedure that doesn't works fine?
I think, that must be fixed

Re: KillThread kills program too
Posted: Fri Nov 22, 2024 4:29 am
by TI-994A
Sergey wrote: Thu Nov 21, 2024 4:53 pm...sometimes it freezes...
Hi Sergey. It freezes because there are no threads to kill. Just give the threads some time to start before trying to kill them.
Code: Select all
Procedure thread(*param)
Debug "thread started!"
Delay(1000)
EndProcedure
For count = 1 To 1000
thread = CreateThread(@thread(), 0)
If thread
Debug Str(count) + ". thread: " + thread
Delay(5)
If IsThread(thread)
KillThread(thread)
Debug "thread killed!"
EndIf
EndIf
Debug "next"
Next
Re: KillThread kills program too
Posted: Fri Nov 22, 2024 8:58 am
by Sergey
TI-994A, wow, it works! You're genius

Re: KillThread kills program too
Posted: Fri Nov 22, 2024 10:28 am
by NicTheQuick
Sergey wrote: Fri Nov 22, 2024 8:58 am
TI-994A, wow, it works! You're genius
No. It works most of the time. But rarely it might freeze again. If you want to do that properly use Semaphores or Mutexes. So basically use them to make sure the Thread is really running already by setting a variable in the thread that you can read back in the main thread.
Re: KillThread kills program too
Posted: Fri Nov 22, 2024 3:38 pm
by Olli
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).
Re: KillThread kills program too
Posted: Fri Nov 22, 2024 8:45 pm
by mk-soft
KillThread is only used if the thread is really more reactive. Otherwise terminate the thread via a struture or global variable