Introduction
Most non-trivial Android apps use more than one thread, therefore multithreaded programming is essential to Android development. At Android NDK, POSIX Threads (pthreads) is bundled in Android's Bionic C library to support multithreading. This chapter mainly discusses the API functions defined in the pthread.h
and semaphore.h
header files, which can be found under the platforms/android-<API level>/arch-arm/usr/include/
folder of Android NDK.
We will first introduce thread creation and termination. Synchronization is important in all multithreaded applications, therefore we discuss four commonly used synchronization techniques at Android NDK with four recipes, including mutex, conditional variables, reader/writer locks, and semaphore. We then illustrate thread scheduling and finally describe how to manage data for threads.
Being a practical book, we will not cover the theories behind multithreaded programming. Readers are expected to understand the basics of multithreading...