Stop Thread

Everything else that doesn't fall into one of the other PB categories.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Stop Thread

Post by IdeasVacuum »

We have PlaySound(), and we can stop playback immediately with StopSound(). Am I right to think that the Sound Lib runs in a separate thread?

The reason I ask is that I feel the Thread Library is missing a function: Stop Thread. In some situations, stopping a thread that has a repeat loop is too slow if reliant on checking for a global var change, as recommended in the help. The alternative in that case is Kill Thread, which is too abrupt because the thread cannot run it's clean-up. So, a Stop Thread Function would in an ideal world deliver the best of both - stop quickly but complete it's current cycle.

Am I as mad as the mirror suggests?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Stop Thread

Post by netmaestro »

Sometimes KillThread is fine and you don't need anything else but in other cases where your thread is working with objects like images and gadgets, an abrupt killing of the thread could cause a crash (say a StartDrawing was open and the thread was killed before you got to StopDrawing would be one example). In cases like this, I've had good results with this approach:

Code: Select all

Global tid_1, thread_1_running=0

Procedure Thread_1(void)
  thread_1_running = #True
  While thread_1_running
    ; do stuff
  Wend
EndProcedure

tid_1 = CreateThread(@Thread_1(), 0)
; do stuff
; ...
; User closed the window or something, I want the thread to finish gracefully
thread_1_running = #False
WaitThread(tid_1)
I'm not sure if the compiler could provide a catch-all solution for every case or not, I just don't know. Maybe it's possible.
BERESHEIT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Stop Thread

Post by IdeasVacuum »

Hi Netmaestro

That is the method I normally use, as recommended in the help....... but sometimes it isn't good enough.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6175
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Stop Thread

Post by blueznl »

Why do you call it 'too slow' ?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Stop Thread

Post by IdeasVacuum »

...because the global var is changed elsewhere in the app and subject to any kind of potential delay - a delay that probably helped you to decide to create a thread for the most critical part of the process in the first place.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply