CreateThread() then, threadpriority() = "not a thread"

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

CreateThread() then, threadpriority() = "not a thread"

Post by jassing »

Image
(for those that want code)

Code: Select all

	hThread = CreateThread( @FetchImage(), *offender )
	If IsThread(hThread)
		ThreadPriority(hThread,#pb_thread_below_normal)
	EndIf
I know this is not a usable code snippet -- but... randomly, I'm getting "not a thread" when I try to adjust the thread priority.
I have added "Delay(250)" at the very start of FetchImage() to be sure it's not exiting too fast.
I then added the "if IsThread()" bit -- and I"m still getting "not a thread"
Any ideas on why or how to rectify this?
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CreateThread() then, threadpriority() = "not a thread"

Post by idle »

you need to use api's to get the handle

Code: Select all

 ;get priority
 PriorityClass = GetPriorityClass_(GetCurrentProcess_());
 Priority = GetThreadPriority_(GetCurrentThread_());
 ;set priority 
 SetPriorityClass_(GetCurrentProcess_(), #REALTIME_PRIORITY_CLASS);
 SetThreadPriority_(GetCurrentThread_(), #THREAD_PRIORITY_TIME_CRITICAL);
 
 ;reset to normal
 SetThreadPriority_(GetCurrentThread_(),Priority);
 SetPriorityClass_(GetCurrentProcess_(), PriorityClass);

;lock to core 1 
mask.i=1 
If Not IsThread(gx) 
   SetThreadAffinityMask_(GetCurrentThread_(),mask)
   gx = CreateThread(@CheckFreqency(),CheckInterval)
EndIf  

Windows 11, Manjaro, Raspberry Pi OS
Image
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: CreateThread() then, threadpriority() = "not a thread"

Post by jassing »

Thanks -- I'm confused tho -- so the handle returned by CreateThread() can't be used with other functions reliably?

do you know the why of the issue?

In addition to being confused, I'm lost -- that code doesn't seem to do anything after hte createthread() -- which is what I want to do -- is have a handle for the thread that I just created... that code (it's late here, so...) -- seems to handle getting the thread of the "current thread" -- no one just created....
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CreateThread() then, threadpriority() = "not a thread"

Post by infratec »

Hi,

I think idle had missunderstood what you mean.

He showed you how to set the priority of the own thread.

But if you want to change the priority of your created thread, your commands are correct.

But I have no explanation why it fails sometimes in your code.

Bernd
cxAlex
User
User
Posts: 88
Joined: Fri Oct 24, 2008 11:29 pm
Location: Austria
Contact:

Re: CreateThread() then, threadpriority() = "not a thread"

Post by cxAlex »

Maybe your Thread is terminated (finished) between IsThread() and ThreadPriority()? I don't know your thread or how long it is, but it could happen.

Greets, Alex
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: CreateThread() then, threadpriority() = "not a thread"

Post by c4s »

Code: Select all

hThread = CreateThread( @FetchImage(), *offender )
If hThread
  ;...
...This should be enough anyway.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: CreateThread() then, threadpriority() = "not a thread"

Post by netmaestro »

cAlex wrote:Maybe your Thread is terminated (finished) between IsThread() and ThreadPriority()? I don't know your thread or how long it is, but it could happen.
This gets my vote.
c4s wrote:...This should be enough anyway.
No, remember that the thread executes asynchronously. After satisfying "If hThread.." successfully the thread could already be gone by the time ThreadPriority tries to execute.
BERESHEIT
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CreateThread() then, threadpriority() = "not a thread"

Post by infratec »

Hi,

that was also my first impression.
But than I read:
jassing wrote:I have added "Delay(250)" at the very start of FetchImage() to be sure it's not exiting too fast.
So he had checked this already. :(

Bernd
JustinJack
User
User
Posts: 89
Joined: Thu Feb 04, 2010 7:34 am
Location: Decatur, TX
Contact:

Re: CreateThread() then, threadpriority() = "not a thread"

Post by JustinJack »

I think you may need to call ThreadID() to get the OS handle of the thread you created..

Code: Select all

numThread = CreateThread(@misc(), *lParam)
hThread = ThreadID(numThread)
SetThreadPriority_(hThread, #THREAD_PRIORITY_HIGHEST)
PB Help on ThreadID()
Returns the unique ID which identifies the given 'Thread' in the operating system. This function is very useful when another library needs a thread reference.
This result is sometimes also known as 'Handle'. Look at the extra chapter Handles and Numbers for more information.
Post Reply