Setting thread affinity for threads in a native library

997 views
Skip to first unread message

John Dallman

unread,
Feb 19, 2021, 7:34:56 AM2/19/21
to andro...@googlegroups.com
Preface: I am not producing an Android app. I work for a software component business, creating shared libraries, compiled from C and C++ code, for use in third-party customers' apps. I test my libraries in a command-line test harness, which I run in the ADB shell. I am only producing software for 64-bit ARM, because none of the customers want 32-bit code. 

One of my shared libraries can split its long tasks into several smaller packets, and create additional threads to run them in parallel for better performance. This is used on Windows, Mac and Linux. I have not enabled this functionality on Android yet, because customers aren't asking for it, and it would require a lot more routine testing. 

I am designing interfaces to allow the applications that use this library to control the use of additional threads. As far as I've been able to discover, Android does not seem to have functions to control thread affinity, as of NDK21. One can set affinity for the whole process, with sched_setaffinity(), but not for individual threads. 

Have I missed something? Has this been deliberately omitted from Android? 

Thanks,

John Dallman

Elliott Hughes

unread,
Feb 19, 2021, 4:09:51 PM2/19/21
to android-ndk
you just need to pass the thread id pid_t to sched_setaffinity(). see https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for more.

(you may have noticed that we link to man7.org in many of our man pages, but <sched.h> isn't one of them. as penance i'll get that done this afternoon...)

Android *doesn't* have pthread_setaffinity_np(), in part because sched_setaffinity() has been available "forever" and if we added pthread_setaffinity_np() today you wouldn't be able to use it for years anyway and it's just a one-line wrapper around sched_setaffinity().

John Dallman

unread,
Feb 22, 2021, 6:59:44 AM2/22/21
to andro...@googlegroups.com
OK. How do I get the pid_t of a thread other than the current thread? I have their pthread_t identifiers, but don't know how to go from one to the other. 

Thanks,

John

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/00e89413-9a50-449f-be04-e57eb54a66c2n%40googlegroups.com.

enh

unread,
Feb 22, 2021, 12:01:17 PM2/22/21
to android-ndk
if you're targeting API level >= 21, you can use pthread_gettid_np() to convert a pthread_t to a pid_t (for any thread, not just the caller). if you need to support older API levels, the easiest way is to have each pthread call gettid().

yu...@unity3d.com

unread,
Feb 22, 2021, 6:05:18 PM2/22/21
to android-ndk
We're using

int result = syscall(__NR_sched_setaffinity, tid, sizeof(affinity), &affinity);

and it works well. Make sure to pass -1 as the affinity by default and not 0.

enh

unread,
Feb 22, 2021, 6:31:35 PM2/22/21
to android-ndk
new code should just use sched_setaffinity() in libc now. it's been there since API level 14 in 2011 (https://android.googlesource.com/platform/bionic/+/72e6fd42421dca80fb2776a9185c186d4a04e5f7), and the NDK doesn't support targeting anything older than jellybean at this point. but you guys may well have been doing this from before API 14 :-)

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

John Dallman

unread,
Mar 1, 2021, 4:16:04 AM3/1/21
to andro...@googlegroups.com
Thanks, everyone. I'm good now.

John

Reply all
Reply to author
Forward
0 new messages