How to gracefully kill a thread?
How to gracefully kill a thread?
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.
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.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How to gracefully kill a thread?
What's not nice about 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
BERESHEIT
Re: How to gracefully kill a thread?
Don't know.
I guess, there are better ways..
At first I thought about CreateMutex_
I guess, there are better ways..
At first I thought about CreateMutex_
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How to gracefully kill a thread?
Using a global variable such as Thread1_running and using it thus:
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.
Code: Select all
Procedure Thread1(void)
Thread1_running = #True
While Thread1_running
; some code
Wend
EndProcedure
BERESHEIT
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Re: How to gracefully kill a thread?
Or you could just wait till I reply to any thread on this forum as that seems to pretty much kill them as well!


Re: How to gracefully kill a thread?
Rook Zimbabwe aka threadKiller... I like that! 

I may look like a mule, but I'm not a complete ass.
Re: How to gracefully kill a thread?
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..."
Re: How to gracefully kill a thread?
That won't work if you use CreateThread() multiple times on the same procedure. Using a global variable also has this problem.dige
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?
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.netmaestro wrote:Using a global variable such as Thread1_running and using it thus: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.Code: Select all
Procedure Thread1(void) Thread1_running = #True While Thread1_running ; some code Wend EndProcedure
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.
- Rook Zimbabwe
- Addict
- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
Re: How to gracefully kill a thread?
I want Fred ar Fangles to set that as my TAG!!!srod wrote:Rook Zimbabwe aka threadKiller... I like that!

I wonder if I could be the PureBasic Threadkiller!!!

Re: How to gracefully kill a thread?
And fangles would then need to be the Purebasic Sheep s****er!Rook Zimbabwe wrote:I want Fred ar Fangles to set that as my TAG!!!srod wrote:Rook Zimbabwe aka threadKiller... I like that!
I wonder if I could be the PureBasic Threadkiller!!!

I may look like a mule, but I'm not a complete ass.
Re: How to gracefully kill a thread?
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?
I don't understand threads very well but.. why all the Delay() when dealing with threads ?
Re: How to gracefully kill a thread?
A 'sheep shifter'? Srod has lost his marbles...srod wrote: And fangles would then need to be the Purebasic Sheep s****er!![]()

( 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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: How to gracefully kill a thread?
So we can see what's going on. Without it would go too fast.PureLeo wrote:I don't understand threads very well but.. why all the Delay() when dealing with threads ?