KillThread kills program too

Just starting out? Need help? Post your questions and find answers here.
Sergey
User
User
Posts: 60
Joined: Wed Jan 12, 2022 2:41 pm

KillThread kills program too

Post 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)
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: KillThread kills program too

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: KillThread kills program too

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Sergey
User
User
Posts: 60
Joined: Wed Jan 12, 2022 2:41 pm

Re: KillThread kills program too

Post 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 :D
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: KillThread kills program too

Post 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. :lol:

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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Sergey
User
User
Posts: 60
Joined: Wed Jan 12, 2022 2:41 pm

Re: KillThread kills program too

Post by Sergey »

TI-994A, wow, it works! You're genius 8)
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: KillThread kills program too

Post by NicTheQuick »

Sergey wrote: Fri Nov 22, 2024 8:58 am TI-994A, wow, it works! You're genius 8)
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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Olli
Addict
Addict
Posts: 1266
Joined: Wed May 27, 2020 12:26 pm

Re: KillThread kills program too

Post 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).
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: KillThread kills program too

Post by mk-soft »

KillThread is only used if the thread is really more reactive. Otherwise terminate the thread via a struture or global variable
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply