C++11 Threads in Android Toolchain
-
Hello,
I'm trying to use an existing library developed in C++11, it uses std threads. I can't create a C++11 thread in a android app, when the thread constructor runs I get a seg fault error.
My Tool Chain in Windows 7 is:
Qt Creator 3.1.0
Qt 5.2.1
Android SDK for API 18 (Jelly)
Android NDK r9d
Deploying on Samsung Galaxy Note 10.1 2014 editionIt compiles, and even can get the correct result for std::thread::hardware_concurrency().
I tryed to set the flag: APP_CPPFLAGS = -std=c++11 and still same problem.
Any ideas?
-
Did you test your code on other platforms?
-
Multi-Threading depends on support by the Linux Kernel. Your application communicates to the kernel using the C libraries that are shipped as part of the Android operating system.
Let me guess: The std libraries installed on the Android system are not compiled with C++11, so they don't support threads.
It's possibly similiar to Java: A program that has ben compiled with Java 6 or 7 can run on Java 5 as long it does not use any of the new features.
-
Vincent: Yes the code is fine in other platforms.
s.frings: Yes multi-threading depends on support on the kernel, but it does not mean they need to be compiled with C++11, it is not how it works. Android underlying kernel with no doubt has thread support.
I believe the problem may lie on android NDK, some config flag that I'm not aware of. Thats what I'm asking :) My NDK toolchain is configured for GCC 4.8 and android-18.
To test it out, one can simply create a foo method inside a class (could be the main form) and call the thread constructor like:
std::thread newThread(&MyClass::FooMethod, this);
it will crash inside the constructor, in my current configuration.
-
I saw someone encountered the same issue. Perhaps, it is a bug.
You can try to use QThread instread of std thread.
-
Unfortunately I can't use Qt Threads in this module :/ At least not right now.
It is maintained as a pure C++11 module, for reuse in other IDEs/Toolchains.I'm still testing, if anyone knows why it does not work.
I found out that people are able to use std threads when using Android NDK, like "this":http://binglongx.wordpress.com/2013/04/07/c11-on-android/ or "this":http://stackoverflow.com/questions/20021527/android-ndk-r9b-and-compiling-c11
But inside Qt Creator it is not working :/
-
@std::thread t2(&MainWindow::ThreadAction, this, "Thread 2");@
Using a class member function with one std::string parameter to create a thread, I reproduced the error and got two images, one with disassembler and other that shows std tuple line of code that occurs the segmentation fault.
"ScreenShot1_disassembler":http://s16.postimg.org/vn5hlcclx/Qt_Print1.png
"ScreenShot2_tupleError":http://s16.postimg.org/5uogrbowl/Qt_Print2.png
-
I found out the problem ¬¬
Due to an if clause misplaced the detach() was never called...
Always remeber to check for a call to join() or detach() ;)