Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting TThread priority on Android

Tags:

android

delphi

I would like to change the priority of a real time audio thread on Android. If I set TThread.Priority to any value except 0 I get an exception:

EThread: Thread Error: Invalid argument"

How do I properly set the thread priority?

like image 875
Aduui Avatar asked Jan 28 '26 07:01

Aduui


1 Answers

On POSIX systems (like Android), there is a TThread.Policy property that specifies the thread's scheduling policy, which in turn controls how the TThread.Priority property behaves. Most likely, the value you are trying to assign for the Priority does not make sense within the current Policy. For example, a Policy of SCHED_OTHER (the default) only supports a Priority of 0, whereas a Policy of SCHED_FIFO or SCHED_RR support a Priority of 1-99 instead (some POSIX system may even limit them to 32).

So you have to make sure the Policy is set to the desired policy first, then you can change the Priority as needed. Note, however, that some Policy values, such as SCHED_FIFO and SCHED_RR, require root/admin rights in order to change their scheduling Priority.

None of this information is documented on the Embarcadero DocWiki, not even the existence of the TThread.Policy property. I looked at the TThread source code and saw that it is using the POSIX pthread_getschedparam() and pthread_setschedparam() functions to get/set the thread's Policy and Priority values. I then looked looked at online references for those functions, such as this one. So, in your case, pthread_setschedparam() is failing with this error:

EINVAL policy is not a recognized policy, or param does not make sense for the policy.
like image 156
Remy Lebeau Avatar answered Jan 29 '26 23:01

Remy Lebeau