Android NDK和pthread

11

我正在使用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功能是否受限?如何正确地在Android NDK中使用pthread?


1
正如这里所指出的以及手册中提到的:这些函数是非标准GNU扩展;因此在名称中带有"_np"(不可移植)后缀。 Bionic显然不支持它(请参见包含有关pthread实现和限制的Bionic C库概述的NDK文档)。 - deltheil
@deltheil所说的是正确的。我的解决方案是不使用pthread功能。我通过条件编译实现了这一点,因为在Android系统上我无论如何都不使用线程功能。 - sdomen
3个回答

5

Android中的pthread功能是否有限制?

据我所知,是的。

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.

1
链接已失效。 - Ruslan
链接已失效。我也遇到了类似的问题。请帮忙。 - Siva

3
请参阅 https://android.googlesource.com/platform/bionic/+/master/docs/status.md 以了解 Android 版本中包含的内容。此外,您还可以查看 NDK 中的 <pthread.h> 头文件(当前版本在 这里),并查看例如以下条目:
pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);

这表明我们确实拥有非POSIX/非可移植(_np)函数pthread_gettid_np,但它是在API级别21中引入的,因此如果您的代码需要在旧版本上运行,则不能使用它。
基本上,头文件是“哪些函数在哪些API级别中可用”的真正权威来源。
pthread_getaffinity_np的具体情况而言,我们不支持。 不过,您可以结合来自<pthread.h><sched.h>pthread_gettid_npsched_getaffinity

我在各个地方都看到了这个问题。这可能是我见过的最好的答案!谢谢! 以下是我如何在C++线程中使用它:cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(cpu_index, &cpuset); auto tid = pthread_gettid_np(worker_threads_.native_handle()); int rc = sched_setaffinity(tid, sizeof(cpu_set_t), &cpuset); - Yitshak Yarom

0

POSIX线程(pthreads)似乎没有为-host构建模块提供。 至少这是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'

目前唯一的修复方法是在指令前在external/boringssl/Android.mk中加入-lpthread:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接