6

I am trying to compile this simple program with android-ndk-r8b:
jni/hello_jni.cpp

#include <iostream>
#include <thread>

void hello()
{
    std::cout << "Hi i'm a thread!!!" << std::endl;
}

int main()
{
    std::thread th(hello);
    th.join();
    return 0;
}

jni/Application.mk

APP_OPTIM := release
APP_MODULES := hello_thread
APP_STL := gnustl_static

jni/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_CPPFLAGS   += -std=c++0x -frtti

LOCAL_MODULE     := hello_thread
LOCAL_LDLIBS     := -L$(SYSROOT)/usr/lib -pthread
LOCAL_SRC_FILES  := hello_thread.cpp

include $(BUILD_EXECUTABLE)

ndk-build returns me an error arguin that 'thread' is not a member of 'std'. I issued ndk-build -n to get the compilation command and issued it alone in my shell:

/home/evigier/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ -MMD -MP -MF /home/evigier/eclipse_workspace/hello_thread/obj/local/armeabi/objs/hello_thread/hello_thread.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__  -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include -I/home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include -I/home/evigier/eclipse_workspace/hello_thread/jni -DANDROID  -Wa,--noexecstack -std=c++0x -frtti  -O2 -DNDEBUG -g   -I/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include -c  /home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp -o /home/evigier/eclipse_workspace/hello_thread/obj/local/armeabi/objs/hello_thread/hello_thread.o 
Compile++ thumb  : hello_thread <= hello_thread.cpp
In file included from /home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/stdio.h:55:0,
                 from /home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/wchar.h:33,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/cwchar:46,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/postypes.h:42,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iosfwd:42,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/ios:39,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/ostream:40,
                 from /home/evigier/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/iostream:40,
                 from jni/hello_thread.cpp:4:
/home/evigier/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/sys/types.h:124:9: error: 'uint64_t' does not name a type
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp: In function 'int main()':
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:14:5: error: 'thread' is not a member of 'std'
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:14:17: error: expected ';' before 'th'
/home/evigier/eclipse_workspace/hello_thread/jni/hello_thread.cpp:15:5: error: 'th' was not declared in this scope

I read a lot of threads/questions about POSIX threads and C++ threads, but still cannot find my answer. My arm-linux-androideabi/include/c++/4.6/thread file defines class thread in std only:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

They don't seem to be defined in my sdk (c++config.h). But how can I possibly turn them on safely? Do i need to compile my own toolchain to use (non-p)threads? My host computer is :

Linux evigier-ThinkPad-X220 3.0.0-17-generic #30-Ubuntu SMP Thu Mar 8 20:45:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
m-ric
  • 5,621
  • 7
  • 38
  • 51

3 Answers3

7

Android NDK r8b comes with gcc 4.6 and does not contain any implementation of C++11 threads. You will have to provide your own implementations and possibly build your own standalone gcc toolchain.

Check these pages for C++11 support in gcc:

  1. http://gcc.gnu.org/projects/cxx0x.html
  2. http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x

POSIX threads is a completely different story and you can use them in Android NDK. You will need at least pthread_create(), pthread_detach() and pthread_join() calls to do it.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • 1
    Thanks Sergey. Why then did android folk put include/c++/4.6/thread header file in the NDK? How do you do some multithreading stuff with the NDK in android without ? Do you use POSIX threads? – m-ric Jul 31 '12 at 13:27
  • I use POSIX threads in Android. They work just fine. I have no idea about the new headers in the 4.6 toolchain. – Sergey K. Jul 31 '12 at 13:31
  • Ok thanks. @commoncpp@ provides class Thread, I will try that. – m-ric Jul 31 '12 at 13:45
  • 2
    This has been fixed since then. Only add `NDK_TOOLCHAIN_VERSION=4.8` or `NDK_TOOLCHAIN_VERSION=clang` to your **Application.mk**. http://stackoverflow.com/questions/23911019/setting-up-c11-for-adt-eclipse – Alex Cohn Jun 02 '14 at 04:38
5

Updating here because this was the top result for my std::this_thread problem:

Threading support has been improved in the latest r8e NDK release (March 2013). Ensure that you are using the latest NDK and toolchain and your problems may be trivially solved.

Edit: The revision change is listed simply as "Enabled threading support in GCC/MIPS toolchain." At a minimum it added support for std::this_thread::sleep_for. If someone else knows of more detailed documentation of the extent of the new support please link it.

idle
  • 1,117
  • 9
  • 17
2

The STL thread library is inspired by the boost library, which you can compile for android OS. Hence, compile Boost.Thread to get an alternative, but similar, implementation.

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • I didn't use Boost but [commoncpp2-1.8.1](http://www.gnu.org/software/commoncpp) which features thread implementation. Few changes required to build it in Android ndk. – m-ric Sep 11 '12 at 22:02
  • 1
    you didn't, but you can, if you want `boost::thread`. An approximation of `std::thread` – user1095108 Sep 13 '12 at 10:08