Android NDK and pthread

I am compiling a Qt / C ++ project with a standalone toolchain for Android NDK. Ive created a standalone toolchain with make-standalone-toolchain.sh --arch = arm --toolchain = arm-linux-androideabi-4.9 --platform = android-21 command. NDK version - android-ndk-r10e. The target project uses some functions from the 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 checked the pthread header included in the ndk toolchain, and I did not find the pthread_getaffinity_np function declaration.

Is pthread function for Android limited? How to use pthread with Android NDK?

+7
source share
3 answers

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. 
+3
source

POSIX threads (pthreads) do not seem to be provided for -host build modules . at least here is the error for building the libcrypto-host module:

 out/host/linux-x86/obj/SHARED_LIBRARIES/libcrypto-host_intermediates/src/crypto/thread_pthread.o: In function `thread_local_init': /media/compilation/projects/android/beagle2/external/boringssl/src/crypto/thread_pthread.c:112: undefined reference to `pthread_key_create' 

and the only way to fix it so far is to add -lpthread inside external / boringssl / Android.mk before the directive:

 include $(BUILD_HOST_SHARED_LIBRARY) 

Example:

 # Host shared library include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_MODULE := libcrypto-host LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include LOCAL_MULTILIB := both LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -Wno-unused-parameter LOCAL_CFLAGS += -DOPENSSL_NO_ASM LOCAL_LDLIBS += -lpthread include $(LOCAL_PATH)/crypto-sources.mk include $(BUILD_HOST_SHARED_LIBRARY) 
0
source

See https://android.googlesource.com/platform/bionic/+/master/docs/status.md our official documents about what version of Android.

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

 pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21); 

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

Mostly the headers are the canonical source of the truth for "what features are available at which API levels?"

for the specific case pthread_getaffinity_np not, we do not support it. You can combine pthread_gettid_np from <pthread.h> and sched_getaffinity from <sched.h> .

0
source

All Articles