Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK and pthread

I'm compiling Qt/C++ project with android NDK standalone toolchain. I’ve created standalone toolchain with make-standalone-toolchain.sh --arch=arm --toolchain=arm-linux-androideabi-4.9 --platform=android-21 command. NDK version is android-ndk-r10e. Target project uses some functions from pthread library. At compile time, I get the following error:

error: 'pthread_getaffinity_np' was not declared in this scope
const int err = pthread_getaffinity_np(_pthreadId, sizeof(cpu_set_t), &cpuSetMask);
compilation terminated due to -Wfatal-errors.

I've checked the header of pthread included in ndk toolchain and I did not find the declaration of pthread_getaffinity_np function.

Is pthread functionality for Android limited? How to use pthread with Android NDK properly?

like image 641
sdomen Avatar asked Jun 12 '15 11:06

sdomen


2 Answers

See https://android.googlesource.com/platform/bionic/+/master/docs/status.md for our official docs about what is in which Android version.

you can also look at the <pthread.h> header in the NDK (current version here) and see for example entries like:

pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);

this shows that we do have the non-POSIX/non-portable (_np) function pthread_gettid_np, but that it was introduced in API level 21, so if your code needs to run on older releases you can't use it.

basically the headers are the canonical source of truth for "which functions are available in which API levels?".

for the specific case of pthread_getaffinity_np, no, we don't support that. you can combine pthread_gettid_np from <pthread.h> and sched_getaffinity from <sched.h> though.

like image 84
Elliott Hughes Avatar answered Sep 28 '22 08:09

Elliott Hughes


Is pthread functionality for Android limited?

AFAIK, Yes.

http://mobilepearls.com/labs/native-android-api/#pthreads
https://web.archive.org/web/20180602101341/http://mobilepearls.com/labs/native-android-api/#pthreads

POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no
additional linking (-lpthreads) is necessary. It does not implement full
POSIX threads functionality and leaves out support for read/write locks,
pthread_cancel(), process-shared mutexes and condition variables as well as
other more advanced features. Read the bionic OVERVIEW.txt for more
information.

TLS, thread-local storage, is limited to 59 pthread_key_t slots available
to applications, lower than the posix minimum of 128.
like image 33
Kazuki Sakamoto Avatar answered Sep 28 '22 07:09

Kazuki Sakamoto