This recipe discusses how to use pthread mutex at Android NDK.
The following steps help to create an Android project that demonstrates the usage of pthread mutex:
Create an Android application named
NativeThreadsMutex
. Set the package name ascookbook.chapter6.nativethreadsmutex
. 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 NativeThreadsMutex, select Android Tools | Add Native Support.
Add a Java file named
MainActivity.java
under thecookbook.chapter6.nativethreadsmutex
package. This Java file simply loads the nativeNativeThreadsMutex
library and calls the nativejni_start_threads
method.Add two files named
mylog.h
andNativeThreadsMutex.cpp
in thejni
folder.NativeThreadsMutex.cpp
contains the code to start two threads. The two threads will update a shared counter. A part of the code is shown...