Android NDK和pthread

时间:2015-06-12 11:18:09

标签: android c++ qt android-ndk pthreads

我正在使用android NDK独立工具链编译Qt / C ++项目。我用make-standalone-toolchain.sh创建了独立工具链--arch = arm --toolchain = arm-linux-androideabi-4.9 --platform = android-21命令。 NDK版本是android-ndk-r10e。目标项目使用pthread库中的一些函数。在编译时,我收到以下错误:

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.

我已经检查了ndk工具链中包含的pthread的标题,但我没有找到pthread_getaffinity_np函数的声明。

Android的pthread功能是否有限?如何正确使用pthread与Android NDK?

3 个答案:

答案 0 :(得分:3)

Is pthread functionality for Android limited?

AFAIK,是的。

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.

答案 1 :(得分:0)

似乎没有为 -host 构建模块提供POSIX线程(pthreads)。 至少这里是libcrypto-host模块构建的错误:

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'

到目前为止修复它的唯一方法是在里面添加-lpthread 指令前的 external / boringssl / Android.mk

include $(BUILD_HOST_SHARED_LIBRARY)

示例:

# 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)

答案 2 :(得分:0)

请参阅https://android.googlesource.com/platform/bionic/+/master/docs/status.md,以获取有关哪个Android版本的正式文档。

您还可以查看NDK(当前版本为here)中的<pthread.h>标头,并查看例如以下条目:

pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);

这表明我们确实具有非POSIX / non-portable(_np)函数pthread_gettid_np,但是它是在API级别21中引入的,因此如果您的代码需要在较旧的版本上运行释放您无法使用的内容。

基本上,标头是“哪些功能在哪些API级别可用?”的规范的真实来源。

对于pthread_getaffinity_np的特定情况,不,我们不支持。您可以将pthread_gettid_np的{​​{1}}和<pthread.h>的{​​{1}}组合起来。