Pthread Priority

2,146 views
Skip to first unread message

Nilly

unread,
Oct 8, 2010, 2:15:06 AM10/8/10
to android-ndk
Hi All,

Is any one using pthread in native code?
I have tried using it but it seems priority is not making any
difference.
What scheduling android uses? I am setting RR (round robin).

Regards,
Niral

David Whittaker

unread,
Oct 8, 2010, 7:06:21 AM10/8/10
to andro...@googlegroups.com

Experimentally, it seems like Android's scheduler is a very simple round robin scheduler that runs a single thread until it sleeps, then the next until it sleeps, and so on.  If you are completely loading the processor to 100%, then there is no guarantee how long it will take to come back from a sleep call, because everything that is scheduled to process must run before you get back to the front of the line.  I've seen sleep(1)s that take 5 seconds to return from.

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>

niral patel

unread,
Oct 8, 2010, 1:44:41 PM10/8/10
to andro...@googlegroups.com
Ok. even I have read in this group that we cant set priority in
pthread from application as it not run as root.
So is there any way to set priority of a thread.
May be by using other APIs all together and not using pthread?

Regards,
NIral

On Fri, Oct 8, 2010 at 4:36 PM, David Whittaker <dpwhi...@gmail.com> wrote:
> Experimentally, it seems like Android's scheduler is a very simple round
> robin scheduler that runs a single thread until it sleeps, then the next
> until it sleeps, and so on.  If you are completely loading the processor to
> 100%, then there is no guarantee how long it will take to come back from a
> sleep call, because everything that is scheduled to process must run before
> you get back to the front of the line.  I've seen sleep(1)s that take 5
> seconds to return from.
>

Dianne Hackborn

unread,
Oct 8, 2010, 2:07:36 PM10/8/10
to andro...@googlegroups.com
Um, no, it is the standard Linux scheduler.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

niral patel

unread,
Oct 8, 2010, 2:15:08 PM10/8/10
to andro...@googlegroups.com
No I am talking about android APIs thread_db.


On Fri, Oct 8, 2010 at 11:37 PM, Dianne Hackborn <hac...@android.com> wrote:
> Um, no, it is the standard Linux scheduler.
>
> On Fri, Oct 8, 2010 at 4:06 AM, David Whittaker <dpwhi...@gmail.com>
> wrote:
>>
>> Experimentally, it seems like Android's scheduler is a very simple round
>> robin scheduler that runs a single thread until it sleeps, then the next
>> until it sleeps, and so on.  If you are completely loading the processor to
>> 100%, then there is no guarantee how long it will take to come back from a
>> sleep call, because everything that is scheduled to process must run before
>> you get back to the front of the line.  I've seen sleep(1)s that take 5
>> seconds to return from.
>>

niral patel

unread,
Oct 8, 2010, 2:17:13 PM10/8/10
to andro...@googlegroups.com
apart from pthread...is there any way to create thread with priority?

fadden

unread,
Oct 8, 2010, 4:09:38 PM10/8/10
to android-ndk
On Oct 8, 10:44 am, niral patel <niral.patel....@gmail.com> wrote:
> Ok. even I have read in this group that we cant set priority in
> pthread from application as it not run as root.
> So is there any way to set priority of a thread.

You can set the priority of a thread so long as you are giving it a
below-normal value.

You can't give your threads higher priority or change the scheduling
class without appropriate permissions.

If you wanted to set up your application so that some threads are
prioritized over others, you can do everything you need with just
this. If you wanted to make your threads more important than threads
in other apps, this will not help.

Dianne Hackborn

unread,
Oct 8, 2010, 6:14:21 PM10/8/10
to andro...@googlegroups.com
On Fri, Oct 8, 2010 at 11:15 AM, niral patel <niral.patel.tel@gmail.com> wrote:
No I am talking about android APIs thread_db.

The threads are run by Linux.  I don't care what API you are using, ultimately it is Linux scheduling the threads, and as I said Android uses the stock Linux scheduler, not a "very simple round robin scheduler that runs a single thread until it sleeps."

David Whittaker

unread,
Oct 8, 2010, 11:12:19 PM10/8/10
to andro...@googlegroups.com

Well, that was all from just experimenting with threading under a heavy load (decoding video).  Maybe this is how the Linux kernel is supposed to schedule threads under load, but I noticed when I called sleep(1) from my audio decoding thread, it didn't return until my video decoding thread slept, even if that was 5 seconds later.   Now, there's always the possibility of a bug in my code, but the test was just two timing calls with a sleep between them while the other thread decoded video, so there wasn't much room for bugs.

David Turner

unread,
Oct 9, 2010, 11:43:02 AM10/9/10
to andro...@googlegroups.com
thread_db is not about changing thread priorities, or playing with the scheduler.
It's a small library which purpose is only to be used by gdb / gdbserver to list / access the threads during debugging.

Even if you used some of the functions there, it would not grant your application process special privileges anyway.

Really, I'm going to remove it from the NDK, it's totally useless to applications, and our gdbserver binaries are statically linked anyway (i.e. the /system/lib/libthread_db.so is in effect useless, so don't rely on it).

Nilly

unread,
Oct 10, 2010, 11:36:39 PM10/10/10
to android-ndk
So there is no way to set priority to the therad in native.
To add I am calling this native function from java application.

Regards,
Niral

On Oct 9, 8:43 pm, David Turner <di...@android.com> wrote:
> thread_db is not about changing thread priorities, or playing with the
> scheduler.
> It's a small library which purpose is only to be used by gdb / gdbserver to
> list / access the threads during debugging.
>
> Even if you used some of the functions there, it would not grant your
> application process special privileges anyway.
>
> Really, I'm going to remove it from the NDK, it's totally useless to
> applications, and our gdbserver binaries are statically linked anyway (i.e.
> the /system/lib/libthread_db.so is in effect useless, so don't rely on it).
>
> On Fri, Oct 8, 2010 at 8:15 PM, niral patel <niral.patel....@gmail.com>wrote:
>
>
>
> > No I am talking about android APIs thread_db.
>
> > On Fri, Oct 8, 2010 at 11:37 PM, Dianne Hackborn <hack...@android.com>
> > wrote:
> > > Um, no, it is the standard Linux scheduler.
>
> > > On Fri, Oct 8, 2010 at 4:06 AM, David Whittaker <dpwhitta...@gmail.com>
> > > wrote:
>
> > >> Experimentally, it seems like Android's scheduler is a very simple round
> > >> robin scheduler that runs a single thread until it sleeps, then the next
> > >> until it sleeps, and so on.  If you are completely loading the processor
> > to
> > >> 100%, then there is no guarantee how long it will take to come back from
> > a
> > >> sleep call, because everything that is scheduled to process must run
> > before
> > >> you get back to the front of the line.  I've seen sleep(1)s that take 5
> > >> seconds to return from.
>
> > >> On Oct 8, 2010 3:29 AM, "Nilly" <niral.patel....@gmail.com> wrote:
> > >> > Hi All,
>
> > >> > Is any one using pthread in native code?
> > >> > I have tried using it but it seems priority is not making any
> > >> > difference.
> > >> > What scheduling android uses? I am setting RR (round robin).
>
> > >> > Regards,
> > >> > Niral
>
> > >> > --
> > >> > You received this message because you are subscribed to the Google
> > >> > Groups "android-ndk" group.
> > >> > To post to this group, send email to andro...@googlegroups.com.
> > >> > To unsubscribe from this group, send email to
> > >> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > >> > For more options, visit this group at
> > >> >http://groups.google.com/group/android-ndk?hl=en.
>
> > >> --
> > >> You received this message because you are subscribed to the Google
> > Groups
> > >> "android-ndk" group.
> > >> To post to this group, send email to andro...@googlegroups.com.
> > >> To unsubscribe from this group, send email to
> > >> android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/android-ndk?hl=en.
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > hack...@android.com
>
> > > Note: please don't send private questions to me, as I don't have time to
> > > provide private support, and so won't reply to such e-mails.  All such
> > > questions should be posted on public forums, where I and others can see
> > and
> > > answer them.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "android-ndk" group.
> > > To post to this group, send email to andro...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.- Hide quoted text -
>
> - Show quoted text -

alan

unread,
Oct 11, 2010, 5:27:19 AM10/11/10
to android-ndk
you could always create the thread in java and set the priority then
call back into native for the body of the thread
> > >http://groups.google.com/group/android-ndk?hl=en.-Hide quoted text -

Dianne Hackborn

unread,
Oct 11, 2010, 3:13:43 PM10/11/10
to andro...@googlegroups.com
You can just use the standard Unix function setpriority().  This is the same thing that the Java APIs use for their implementation.

That said, if you want your thread to *really* be a background thread, Android also makes use of Linux's cgroup facility to put threads in different cgroups based on their  priority.  Unfortunately there isn't an API for this in the NDK, so I would recommend just doing a simple JNI call to android.os.Process#setThreadPriority with the desired standard priority constant defined in Process.  This is a static method in Process, so the JNI call is fairly trivial.

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer

Nilly

unread,
Oct 12, 2010, 4:59:46 AM10/12/10
to android-ndk
but still setpriority() function also need root permission right?
As I need to use it (every thing) from java application i wont be
running application with root permission.
So probably thread creation from java is only option right?
Actually i dont want to do thread creation from java code as I want to
keep java and native code separate and very less dependent.

Regards,
Niral
> > <android-ndk%2Bunsubscribe@googlegr­oups.com>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/android-ndk?hl=en.-Hidequoted text -
>
> > > > - Show quoted text -
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.- Hide quoted text -

Nilly

unread,
Oct 12, 2010, 6:22:55 AM10/12/10
to android-ndk
ok!!!

> so I would recommend just doing a simple JNI call
> to android.os.Process#setThreadPriority with the desired standard priority
> constant defined in Process. This is a static method in Process, so the JNI
> call is fairly trivial.

u mean to say i will create thread using pthread in native and set
priority using android.os.Process#setThreadPriority in native?
is it what you meant?

correct me if i am wrong.

Regards,
Niral
> > > > > >http://groups.google.com/group/android-ndk?hl=en.-Hidequotedtext -
>
> > > > > - Show quoted text -
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "android-ndk" group.
> > > To post to this group, send email to andro...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­­oups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > Dianne Hackborn
> > Android framework engineer
> > hack...@android.com
>
> > Note: please don't send private questions to me, as I don't have time to
> > provide private support, and so won't reply to such e-mails.  All such
> > questions should be posted on public forums, where I and others can see and
> > answer them.- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -

Tim Mensch

unread,
Oct 12, 2010, 10:55:33 AM10/12/10
to andro...@googlegroups.com
On 10/12/2010 4:22 AM, Nilly wrote:
> ok!!!
>
> > so I would recommend just doing a simple JNI call
> > to android.os.Process#setThreadPriority with the desired standard priority
> > constant defined in Process. This is a static method in Process, so the JNI
> > call is fairly trivial.
>
> u mean to say i will create thread using pthread in native and set
> priority using android.os.Process#setThreadPriority in native?
> is it what you meant?
>
> correct me if i am wrong.
setpriority() should work to reduce the priority of a thread even if
you're not root. See the Linux man page:

http://linux.die.net/man/2/setpriority

Note that the terminology on the page (and in Linux) is reversed from
what you might expect: "Lower" priority values mean that it's a higher
priority. And the page notes that "Only the superuser may lower priorities."

Tim

Olivier Guilyardi

unread,
Oct 12, 2010, 11:29:54 AM10/12/10
to andro...@googlegroups.com
On 10/12/2010 12:22 PM, Nilly wrote:
> ok!!!
>
>> so I would recommend just doing a simple JNI call
>> to android.os.Process#setThreadPriority with the desired standard priority
>> constant defined in Process. This is a static method in Process, so the JNI
>> call is fairly trivial.

I didn't know about that. That's very interesting. I'm currently doing some
usleep magic to prevent one of my threads from overloading the system. Setting
the priority would be smarter.

> u mean to say i will create thread using pthread in native and set
> priority using android.os.Process#setThreadPriority in native?
> is it what you meant?

I think the idea is:

- create a thread with pthread
- within this native thread, call Process.myTid() to obtain the thread id
- then call Process.setThreadPriority() with the said id and desired priority

Is this correct?

--
Olivier

fadden

unread,
Oct 12, 2010, 2:03:09 PM10/12/10
to android-ndk
On Oct 12, 8:29 am, Olivier Guilyardi <l...@samalyse.com> wrote:
> I think the idea is:
>
> - create a thread with pthread
> - within this native thread, call Process.myTid() to obtain the thread id
> - then call Process.setThreadPriority() with the said id and desired priority

Only if you like doing it the hard way. :-)

As hackbod noted earlier:

| You can just use the standard Unix function setpriority(). This is
the same
| thing that the Java APIs use for their implementation.

The setpriority system call changes the "nice" value of the process.
You can admire your work with "ps -p -t" on the device.

As another poster mentioned, higher values are "more nice" and
indicate lower priority.

A thread that is spinning on the CPU will continue to spin and eat
battery, even at a lower priority. It will just give up time to other
threads more readily.

Dianne Hackborn

unread,
Oct 12, 2010, 9:30:33 PM10/12/10
to andro...@googlegroups.com
Though again, setpriority() won't change the cgroup.  To be polite, you really want to have the cgroup changed to background, so you can't interfere with foreground/UI threads.  The Java API will do this; unfortunately there is no interface in the NDK for the native code it uses for its implementation.

Also as far as who can do what -- security in Android is done at the process level.  Java can't do anything more than native code, nor can native code do anything more than Java.  The only differences between them are what APIs are available that provide functionality that the platform promises to support in the future and across different devices.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer

Olivier Guilyardi

unread,
Oct 13, 2010, 6:22:02 AM10/13/10
to andro...@googlegroups.com
Avoiding UI slowdown/freezes, when doing hard work in the background, is
precisely what I'd change the thread priority for.

Therefore I'd better use Process.setThreadPriority() than setpriority(), right?
You two seem to disagree.

Olivier

On 10/13/2010 03:30 AM, Dianne Hackborn wrote:
> Though again, setpriority() won't change the cgroup. To be polite, you
> really want to have the cgroup changed to background, so you can't
> interfere with foreground/UI threads. The Java API will do this;
> unfortunately there is no interface in the NDK for the native code it
> uses for its implementation.
>
> Also as far as who can do what -- security in Android is done at the
> process level. Java can't do anything more than native code, nor can
> native code do anything more than Java. The only differences between
> them are what APIs are available that provide functionality that the
> platform promises to support in the future and across different devices.
>
> On Tue, Oct 12, 2010 at 11:03 AM, fadden <fad...@android.com
> <mailto:fad...@android.com>> wrote:
>
> On Oct 12, 8:29 am, Olivier Guilyardi <l...@samalyse.com

> <mailto:l...@samalyse.com>> wrote:
> > I think the idea is:
> >
> > - create a thread with pthread
> > - within this native thread, call Process.myTid() to obtain the
> thread id
> > - then call Process.setThreadPriority() with the said id and
> desired priority
>
> Only if you like doing it the hard way. :-)
>
> As hackbod noted earlier:
>
> | You can just use the standard Unix function setpriority(). This is
> the same
> | thing that the Java APIs use for their implementation.
>
> The setpriority system call changes the "nice" value of the process.
> You can admire your work with "ps -p -t" on the device.
>
> As another poster mentioned, higher values are "more nice" and
> indicate lower priority.
>
> A thread that is spinning on the CPU will continue to spin and eat
> battery, even at a lower priority. It will just give up time to other
> threads more readily.
>
> --
> You received this message because you are subscribed to the Google
> Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com

> <mailto:andro...@googlegroups.com>.


> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com

> <mailto:android-ndk%2Bunsu...@googlegroups.com>.


> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.
>
>
>
>
> --
> Dianne Hackborn
> Android framework engineer

> hac...@android.com <mailto:hac...@android.com>


>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails. All such
> questions should be posted on public forums, where I and others can see
> and answer them.
>

Reply all
Reply to author
Forward
0 new messages