No luck compiling __thread using ndk clang 3.4/3.5

拈花ヽ惹草 提交于 2019-12-02 04:24:51

__thread style TLS (similarly C++11 style thread_local) is not currently supported by the Android linker (we're working on it though). For now you'll have to use pthread_getspecific and pthread_setspecific.

EDIT: Starting with NDK r12, Clang has support for emulated TLS. While the above is still technically true (the linker on the device does not support this), the compiler can replace builtin TLS support with pthread calls on your behalf.

NB: Android still only supports thread_local for objects with trivial destructors until r14. See discussions on https://github.com/android-ndk/ndk/issues/156#issuecomment-231878690 and https://github.com/android-ndk/ndk/issues/216.

Firstly, you should avoid using __thread as it may not be portable.

Here is what gnu gcc says here https://gcc.gnu.org/onlinedocs/gcc-4.8.3/gcc/Thread-Local.html :

The runtime model GCC uses to implement this originates in the IA-64 processor-specific ABI, but has since been migrated to other processors as well. It requires significant support from the linker (ld), dynamic linker (ld.so), and system libraries (libc.so and libpthread.so), so it is not available everywhere.

And from wikipédia http://en.wikipedia.org/wiki/Thread-local_storage#C.2B.2B :

C++0x introduces the thread_local keyword. Aside that, various C++ compiler implementations provide specific ways to declare thread-local variables:

Sun Studio C/C++, IBM XL C/C++, GNU C and Intel C/C++ (Linux systems) use the syntax:

    __thread int number;
Visual C++, Intel C/C++ (Windows systems), Borland C++ Builder and Digital Mars C++ use the syntax:

    __declspec(thread) int number;
 Borland C++ Builder also supports the syntax:

    int __thread number;

So it may work with thread_local key word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!