Some flags for CreateThread
Posted: Thu Feb 09, 2006 12:28 pm
I've got a problem where when I create a thread, it starts running immediately,
which I suspect is the normal behaviour? But even if I pause it immediately,
I still sometimes get an error because the thread has started before the
PauseThread command has kicked in. So maybe CreateThread could have
another flag to tell it pause the thread when created? Here's some code
which demonstrates my problem:
If you run this, 9 times out of 10 you'll see "Main loop" in the Debug Output,
which is great. But occasionally, you'll see "Thread" there first, because
the thread has started before PauseThread in the If/EndIf check is done.
This causes my app to crash, because the thread is trying to do something
when my Main Loop isn't ready yet... even though I've paused the thread
immediately after creating it.
So, I was thinking, maybe we could have an optional flag to pause the
thread as soon as it's created, to prevent this happening? Perhaps like:
And while I think of it, maybe another flag to set the priority of the thread
too? Like: #PB_Thread_Low, #PB_Thread_BelowNormal, #PB_Thread_Normal,
#PB_Thread_AboveNormal, #PB_Thread_High, #PB_Thread_RealTime?
So to create a new thread that's paused upon creation with a high priority:
Too much?
I think it'd be very handy.
EDIT: Okay, I know I can easily just do it like this...
...but there may be situations where pausing it like that won't do (I can't
think of any), so if it's easy enough to put in, yeah, go for it.
Or at least
put in the priority flags?
which I suspect is the normal behaviour? But even if I pause it immediately,
I still sometimes get an error because the thread has started before the
PauseThread command has kicked in. So maybe CreateThread could have
another flag to tell it pause the thread when created? Here's some code
which demonstrates my problem:
Code: Select all
Global th
Procedure MyThread()
Repeat
Debug "Thread"
PauseThread(th)
ForEver
EndProcedure
th=CreateThread(@MyThread(),0)
If th=0 : End : Else : PauseThread(th) : EndIf
Debug "Main loop"
Repeat : Sleep_(1) : ForEver
which is great. But occasionally, you'll see "Thread" there first, because
the thread has started before PauseThread in the If/EndIf check is done.
This causes my app to crash, because the thread is trying to do something
when my Main Loop isn't ready yet... even though I've paused the thread
immediately after creating it.

So, I was thinking, maybe we could have an optional flag to pause the
thread as soon as it's created, to prevent this happening? Perhaps like:
Code: Select all
th=CreateThread(@MyThread(),0,#PB_Thread_Paused)
too? Like: #PB_Thread_Low, #PB_Thread_BelowNormal, #PB_Thread_Normal,
#PB_Thread_AboveNormal, #PB_Thread_High, #PB_Thread_RealTime?
So to create a new thread that's paused upon creation with a high priority:
Code: Select all
th=CreateThread(@MyThread(),0,#PB_Thread_Paused|#PB_Thread_High)

EDIT: Okay, I know I can easily just do it like this...
Code: Select all
Procedure MyThread()
Repeat
PauseThread(th)
Debug "Thread"
ForEver
EndProcedure
think of any), so if it's easy enough to put in, yeah, go for it.

put in the priority flags?
