Executing asynchronous work on a native thread
The Android NDK is bundled with the POSIX thread C API that provides an API to create and destroy native threads, native mutual exclusion synchronization primitives, named mutexes, and condition variables, that like Java monitors, allow threads to wait until a change in a resource happens. Apart from this global API, the developer also has access to a higher level C++11 thread API available on clang
and gnu_stl
C++ Runtimes.
Since both of these frameworks offer the same kind of concurrent functionalities we will use C++11 thread framework for its simplicity and similarity with the Java Thread API.
First, let's update our ndk build.gradle
to use the clang C++ Runtime that supports the thread API that we are going to use in our following code examples:
ndk { moduleName "mylib" stl "c++_shared" cppFlags.add("-frtti") cppFlags.add("-exceptions) }
Attaching and detaching native threads from JVM
In order to interact with our JVM and execute background...