O'Reilly logo

Android NDK Beginner's Guide - Second Edition by Sylvain Ratabouil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. In Store.cpp, include unistd.h, which gives access to the sleep() 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 a StoreWatcher structure.

  2. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required