NDK r9c - does not support std :: future
I have the following source code using std :: future.
#include<future>
std::future<int> A;
However, when I try to compile it with NDK_TOOLCHAIN_VERSION: = 4.8 in the Application.mk file, it throws the error "std :: future A" is of incomplete type, etc. "
I tried looking for future.h in the android-ndk-r9c folder, however I could not find it. Does anyone know if the NDK supports std :: future? If so, which version has this support?
source to share
It seems std :: future is not available with any stl library and compilers with ndk-r9c.
Starting with ndk r9d you can use the llvm libc ++ library. The support is described as "experimental" but seems to work well enough.
Instead of your currently selected STL library (gnustl, I guess?), Use c++_shared
or c++_static
in your Application.mk, for example:
APP_ABI := all
APP_STL := c++_shared
NDK_TOOLCHAIN_VERSION := clang
If you run into some runtime issues with the static version, you can add LIBCXX_FORCE_REBUILD := true
inside Application.mk to force it to restore correctly.
source to share
@pree: I am currently experimenting with the same settings in Application.mk:
APP_ABI := armeabi-v7a APP_STL := gnustl_static APP_CPPFLAGS := -frtti --std=c++11 NDK_TOOLCHAIN_VERSION := 4.8
However, while my code works fine on a Nexus 10, the app freezes on my LG phone.
EDIT: I have narrowed down the problem. The code I'm running is the following:
auto task1 = std::packaged_task<void()>([]() {
LOG_INFO("Task1 starting");
sleep(5);
LOG_INFO("Task1 finishing");
});
auto fut1 = task1.get_future();
auto task2 = std::packaged_task<void()>([]() {
LOG_INFO("Task2 starting");
sleep(10);
LOG_INFO("Task2 finishing");
});
auto fut2 = task2.get_future();
std::thread([&](){task1();}).detach();
std::thread([&](){task2();}).detach();
LOG_INFO("waiting for task1");
fut1.wait();
LOG_INFO("waiting for task1 done");
LOG_INFO("waiting for task2");
fut2.wait();
LOG_INFO("waiting for task2 done");
On Nexus 10, this code outputs the following output:
18:14:15.365: waiting for task1
18:14:15.365: Task1 starting
18:14:15.365: Task2 starting
18:14:20.370: Task1 finishing
18:14:20.370: waiting for task1 done
18:14:20.370: waiting for task2
18:14:25.365: Task2 finishing
18:14:25.365: waiting for task2 done
While on another device (LG phone), I get the following output:
18:23:21.999: Task1 starting
18:23:21.999: waiting for task1
18:23:26.999: Task1 finishing
18:23:26.999: waiting for task1 done
18:23:26.999: waiting for task2
18:23:26.999: Task2 starting
18:23:37.178: Task2 finishing
18:23:37.178: waiting for task2 done
This means that on the LG device, the second task waits for the first to complete until it starts. However, the problem comes from std :: packaging_task and not from std :: future, because the following code behaves as expected on both devices:
std::promise<void> promise1;
auto task1 = std::function<void()>([&]() {
LOG_INFO("Task1 starting");
sleep(5);
promise1.set_value();
LOG_INFO("Task1 finishing");
});
auto fut1 = promise1.get_future();
std::promise<void> promise2;
auto task2 = std::function<void()>([&]() {
LOG_INFO("Task2 starting");
sleep(10);
promise2.set_value();
LOG_INFO("Task2 finishing");
});
auto fut2 = promise2.get_future();
std::thread([&](){task1();}).detach();
std::thread([&](){task2();}).detach();
LOG_INFO("waiting for task1");
fut1.wait();
LOG_INFO("waiting for task1 done");
LOG_INFO("waiting for task2");
fut2.wait();
LOG_INFO("waiting for task2 done");
Perhaps I misunderstood something?
source to share