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 a native thread with the PThread POSIX API:
pthread_attr_init()
initializes the necessary data structurepthread_create()
starts the thread... pthread_attr_t lAttributes...