Page 1 of 2

How to gracefully kill a thread?

Posted: Sun May 02, 2010 10:25 pm
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.

Re: How to gracefully kill a thread?

Posted: Sun May 02, 2010 10:26 pm
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?

Re: How to gracefully kill a thread?

Posted: Sun May 02, 2010 10:30 pm
by crackhead
Don't know.
I guess, there are better ways..

At first I thought about CreateMutex_

Re: How to gracefully kill a thread?

Posted: Sun May 02, 2010 10:38 pm
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.

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 3:40 am
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:

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 9:26 am
by srod
Rook Zimbabwe aka threadKiller... I like that! :)

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 10:24 am
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

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 12:07 pm
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)

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 12:56 pm
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
:)

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 3:11 pm
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:

Re: How to gracefully kill a thread?

Posted: Mon May 03, 2010 4:03 pm
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:

Re: How to gracefully kill a thread?

Posted: Tue May 04, 2010 1:05 pm
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

Re: How to gracefully kill a thread?

Posted: Fri May 07, 2010 10:32 am
by PureLeo
I don't understand threads very well but.. why all the Delay() when dealing with threads ?

Re: How to gracefully kill a thread?

Posted: Fri May 07, 2010 10:55 am
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)

Re: How to gracefully kill a thread?

Posted: Fri May 07, 2010 12:14 pm
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.