How to gracefully kill a thread?

Just starting out? Need help? Post your questions and find answers here.
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

How to gracefully kill a thread?

Post by crackhead »

Hi,
I need to kill a thread and I guess KillThread wouldn't be a nice way.
So, how shall I do it?

I could set a global variable and check inside of the thread if it is set to exit, but this also wouldn't be such a nice way

Thanks.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to gracefully kill a thread?

Post by netmaestro »

I could set a global variable and check inside of the thread if it is set to exit, but this also wouldn't be such a nice way
What's not nice about it?
BERESHEIT
crackhead
User
User
Posts: 12
Joined: Tue Apr 06, 2010 9:26 pm

Re: How to gracefully kill a thread?

Post by crackhead »

Don't know.
I guess, there are better ways..

At first I thought about CreateMutex_
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to gracefully kill a thread?

Post by netmaestro »

Using a global variable such as Thread1_running and using it thus:

Code: Select all

Procedure Thread1(void)
  Thread1_running = #True
  While Thread1_running
     ; some code
  Wend
EndProcedure
is an excellent way of making sure the thread is allowed to exit gracefully, that is, not in the middle of some task that will cause leaks or worse if it is interrupted mid-execution. Some people seem to have the idea that global variables are bad things to be avoided at all costs, but this is just not the case. They have some valuable uses and this is one of them.
BERESHEIT
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: How to gracefully kill a thread?

Post by Rook Zimbabwe »

Or you could just wait till I reply to any thread on this forum as that seems to pretty much kill them as well!

:wink:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to gracefully kill a thread?

Post by srod »

Rook Zimbabwe aka threadKiller... I like that! :)
I may look like a mule, but I'm not a complete ass.
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: How to gracefully kill a thread?

Post by dige »

I do it like this:

Code: Select all

Procedure Thread (Arg.i); 0-Start Thread, 1-Quit Thread, 2-Alive Check
  Static Alive.i, flags.b
  
  If Arg = 1
    flags = #True
    ProcedureReturn
  ElseIf Arg = 2
    
    ProcedureReturn Alive
  EndIf
  
  
  Repeat
    ; do something
    Delay(100)
    Alive = ElapsedMilliseconds()
  Until flags = #True
  
  Debug "Thread finished"
EndProcedure

CreateThread (@Thread(), #Null)

Delay(5000)
; Do Alive Check
Debug "Last Update:" + Str(ElapsedMilliseconds() - Thread(2)) + "ms"

Delay(5000)
; Send Quit Signal
Thread(#True)

Delay(5000)
End
"Daddy, I'll run faster, then it is not so far..."
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: How to gracefully kill a thread?

Post by Trond »

dige
That won't work if you use CreateThread() multiple times on the same procedure. Using a global variable also has this problem.

I suggest passing a pointer to a structure (which may be in an array or dynamically allocated). In this structure there is a variable which can be set to indicate that the thread should quit.

Code: Select all

Structure SThreadInfo
  QuitRequest.i
  PbNumber.i
  ID.i
EndStructure

Procedure Thread(*Thread.SThreadInfo)
  While Not *Thread\QuitRequest
    ; Do something
    Delay(100)
  Wend
  Debug Str(*Thread\ID) + " ending."
EndProcedure

Dim Threads.SThreadInfo(3)
For I = 0 To 3
  Threads(I)\ID = I
  Threads(I)\PbNumber = CreateThread(@Thread(), @Threads(I))
Next

Delay(500)
Threads(3)\QuitRequest = 1
Delay(1000)
Threads(0)\QuitRequest = 1
Threads(1)\QuitRequest = 1
Delay(1500)
Threads(2)\QuitRequest = 1
Delay(500)
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: How to gracefully kill a thread?

Post by SFSxOI »

netmaestro wrote:Using a global variable such as Thread1_running and using it thus:

Code: Select all

Procedure Thread1(void)
  Thread1_running = #True
  While Thread1_running
     ; some code
  Wend
EndProcedure
is an excellent way of making sure the thread is allowed to exit gracefully, that is, not in the middle of some task that will cause leaks or worse if it is interrupted mid-execution. Some people seem to have the idea that global variables are bad things to be avoided at all costs, but this is just not the case. They have some valuable uses and this is one of them.
I use globals, and netmaestro's method above, a lot. It works well and I like it. In fact I think I picked it up from some code that netmaestro posted at one time or another. There's nothing wrong with globals either. Tronds method works also and i've used similar too. It depends on what your doing sometimes, and picking the correct way that suits what your attempting to accomplish.

Rook Zimbabwe, well, yeah his method works effectively in a lot of cases. Maybe there should be a 'RookZimbabwe' command in PureBasic like this.

Code: Select all

        
forum_thread = Forum_Thread_ID
       If num_posts=1
          RookZimbabwe(forum_thread); guranteed kill :)
        EndIf
:)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: How to gracefully kill a thread?

Post by Rook Zimbabwe »

srod wrote:Rook Zimbabwe aka threadKiller... I like that! :)
I want Fred ar Fangles to set that as my TAG!!! :D

I wonder if I could be the PureBasic Threadkiller!!! :twisted:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to gracefully kill a thread?

Post by srod »

Rook Zimbabwe wrote:
srod wrote:Rook Zimbabwe aka threadKiller... I like that! :)
I want Fred ar Fangles to set that as my TAG!!! :D

I wonder if I could be the PureBasic Threadkiller!!! :twisted:
And fangles would then need to be the Purebasic Sheep s****er! :twisted:
I may look like a mule, but I'm not a complete ass.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: How to gracefully kill a thread?

Post by AndyMK »

Another way.

Code: Select all

Global ThreadSemaphore = CreateSemaphore()

Procedure MyThread(val)
  
  Debug "Thread Created"
  Repeat
    ;CODE
  Until TrySemaphore(ThreadSemaphore)
  Debug "Thread Closed"
  
EndProcedure

Thread = CreateThread(@MyThread(), 0)

;*** PRESS ESCAPE TO EXIT ***
Repeat
  Delay(100)
Until GetAsyncKeyState_(#VK_ESCAPE)

;*** EXIT CODE ***
If IsThread(Thread)
  SignalSemaphore(ThreadSemaphore)
EndIf
End
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: How to gracefully kill a thread?

Post by PureLeo »

I don't understand threads very well but.. why all the Delay() when dealing with threads ?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: How to gracefully kill a thread?

Post by blueznl »

srod wrote: And fangles would then need to be the Purebasic Sheep s****er! :twisted:
A 'sheep shifter'? Srod has lost his marbles...

8)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: How to gracefully kill a thread?

Post by Trond »

PureLeo wrote:I don't understand threads very well but.. why all the Delay() when dealing with threads ?
So we can see what's going on. Without it would go too fast.
Post Reply