Time for action – running and synchronizing a thread
Let's create a native thread using the POSIX PThread API and attach it to the VM:
- In
Store.cpp
, includeunistd.h
, which gives access to thesleep()
function:#include "Store.h" #include <cstdlib> #include <cstring> #include <unistd.h> ...
Implement
startWatcher()
. This method is executed from the UI thread. To do so, first instantiate and initialize aStoreWatcher
structure. - Then, initialize and launch a native thread with the
pthread
POSIX API:StoreWatcher* startWatcher(JavaVM* pJavaVM, Store* pStore, jobject pLock) { StoreWatcher* watcher = new StoreWatcher(); watcher->mJavaVM = pJavaVM; watcher->mStore = pStore; watcher->mLock = pLock; watcher->mRunning = true; ...
Then, initialize and launch ...
Get Android NDK Beginner's Guide - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.