launch a thread only if a core is available, possible ?
- pf shadoko
- Enthusiast
- Posts: 385
- Joined: Thu Jul 09, 2015 9:07 am
launch a thread only if a core is available, possible ?
I would like to know if it is possible to launch a thread only if a core is available.
for my 3D scenery, I modify the level of mesh detail in a thread
no problem if its execution is delayed, but what bothers me is freezing the main thread
currently, I only launch a thread if the number of threads currently running (launched from PB) is less than the number of cores -1
but this sometimes freezes the machine for a few moments, which is annoying for some action games (like car racing).
for my 3D scenery, I modify the level of mesh detail in a thread
no problem if its execution is delayed, but what bothers me is freezing the main thread
currently, I only launch a thread if the number of threads currently running (launched from PB) is less than the number of cores -1
but this sometimes freezes the machine for a few moments, which is annoying for some action games (like car racing).
Re: launch a thread only if a core is available, possible ?
Looks like your threads are programmed without delays and thus bring the processor to a boil and thus no computing power remains for the GUI and system.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
- pf shadoko
- Enthusiast
- Posts: 385
- Joined: Thu Jul 09, 2015 9:07 am
Re: launch a thread only if a core is available, possible ?
yes, that's why I'm asking this question
Re: launch a thread only if a core is available, possible ?
Then you should have a look at your thread processing. Threads should not run in a continuous loop without delay.
If threads are dependent on each other, work with semaphores.
If threads are dependent on each other, work with semaphores.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
- pf shadoko
- Enthusiast
- Posts: 385
- Joined: Thu Jul 09, 2015 9:07 am
Re: launch a thread only if a core is available, possible ?
well, I'll rephrase my question:
is there any way of knowing if a core is available?
is there any way of knowing if a core is available?
Re: launch a thread only if a core is available, possible ?
No,
the threads are distributed by the OS core system. This means that all cores are always used. Some more, some less depending on the omission of the system
the threads are distributed by the OS core system. This means that all cores are always used. Some more, some less depending on the omission of the system
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: launch a thread only if a core is available, possible ?
you can try SetThreadAffinityMask on windows to bind a thread to a logical core.
and also set the priorities but you really should put a delay(0) in the loops, so context switches can be handled nicely
and use a semaphore so the thread can be created then woken up to do job and go back to the wait.
and also set the priorities but you really should put a delay(0) in the loops, so context switches can be handled nicely
and use a semaphore so the thread can be created then woken up to do job and go back to the wait.
Code: Select all
PriorityClass = GetPriorityClass_(GetCurrentProcess_());
Priority = GetThreadPriority_(GetCurrentThread_());
SetPriorityClass_(GetCurrentProcess_(), #REALTIME_PRIORITY_CLASS);
SetThreadPriority_(GetCurrentThread_(), #THREAD_PRIORITY_TIME_CRITICAL);
SetThreadAffinityMask_(GetCurrentThread_(),1) ;<core number?
Re: launch a thread only if a core is available, possible ?
It would be good if the documentation for Delay() is extended to include this important background that you've mentioned here. It would be easy for someone to assume that Delay(0) does nothing, but the Windows API documentation offers this useful pointer in the first sentence :idle wrote: Sun Jun 09, 2024 1:32 am you can try SetThreadAffinityMask on windows to bind a thread to a logical core. and also set the priorities but you really should put a delay(0) in the loops, so context switches can be handled nicely and use a semaphore so the thread can be created then woken up to do job and go back to the wait.
Interesting also, "... to relinquish the remainder of its time slice to any other thread" could suggest Delay(0) is not necessary in order for the context switch to happen, since it is allocated a finite time slice anyway, if I'm to understand that correctly.A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution. Windows XP: A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. This behavior changed starting with Windows Server 2003.
A value of INFINITE indicates that the suspension should not time out.
Source — https://learn.microsoft.com/en-us/windo ... hapi-sleep
Re: launch a thread only if a core is available, possible ?
It's important for threads running on the same logical core. If you say I'm done the other threads on that core can run before your time slice is up. It's no point sitting idle spinning on wait looking around the room looking at the clock and twiddling your thumbs....when another thread could be using that time.
Re: launch a thread only if a core is available, possible ?
If a thread remained idle, in that way Idleidle wrote: Tue Jun 11, 2024 9:43 pm ... If you say I'm done the other threads on that core can run before your time slice is up. It's no point sitting idle spinning on wait looking around the room looking at the clock and twiddling your thumbs....when another thread could be using that time.

I'm sure you're right, that delays are important, but I find that for my own work, which is largely traditional intensive data processing , Delay(0) increases the processing time somewhat.
Re: launch a thread only if a core is available, possible ?
I mean when you have a semaphore or a spin lock at the head of the thread loop, so if your thread can complete within the time slice you can call delay(0) or SwitchToThread_() which appear to be the replacement.PBJim wrote: Wed Jun 12, 2024 6:53 amIf a thread remained idle, in that way Idleidle wrote: Tue Jun 11, 2024 9:43 pm ... If you say I'm done the other threads on that core can run before your time slice is up. It's no point sitting idle spinning on wait looking around the room looking at the clock and twiddling your thumbs....when another thread could be using that time., we’d almost certainly include provision for a delay as a matter of course anyway, but I was thinking in terms of threads that are started specifically to perform a task, complete it promptly and then terminate. Perhaps similar to the OP’s case.
I'm sure you're right, that delays are important, but I find that for my own work, which is largely traditional intensive data processing , Delay(0) increases the processing time somewhat.
I didn't know delay(0) had changed behaviour on windows.
I try my best to be idle, wife has other ideas though

Re: launch a thread only if a core is available, possible ?
Ah, I see what you mean. I'd always do something like this, which enters a delay and only wakes up to check status, as a matter of principle :idle wrote: Wed Jun 12, 2024 7:19 am I mean when you have a semaphore or a spin lock at the head of the thread loop, so if your thread can complete within the time slice you can call delay(0) or SwitchToThread_() which appear to be the replacement.
Code: Select all
waitend.i = ElapsedMilliseconds() + timeout.i
While ElapsedMilliseconds() < waitend.i
Delay(1000)
If TrySemaphore(x)
; Do something here
EndIf
Wend
Re: launch a thread only if a core is available, possible ?
yes so you could do this
so your thread waits until signaled and then bail out so another thread on the same core can run
Code: Select all
Repeat
If WaitSemaphore(x)
; Do something here
EndIf
SwitchToThread_()
Until done