Synchronizing native threads with conditional variables at Android NDK
The previous recipe discusses how to synchronize threads with mutex. This recipe describes how to use conditional variables.
How to do it...
The following steps will help us create an Android project that demonstrates the usage of pthread conditional variables:
Create an Android application named
NativeThreadsCondVar
. Set the package name ascookbook.chapter6.nativethreadscondvar
. Refer to the Loading native libraries and registering native methods recipe in Chapter 2, Java Native Interface for more detailed instructions.Right-click on the project NativeThreadsCondVar, select Android Tools | Add Native Support.
Add a Java file named
MainActivity.java
under the packagecookbook.chapter6.nativethreadscondvar
. This Java file simply loads the native libraryNativeThreadsCondVar
and calls the nativejni_start_threads
method.Add two files named
mylog.h
andNativeThreadsCondVar.cpp
under thejni
folder.NativeThreadsCondVar.cpp...