Synchronizing native cross-platform threads
Synchronization is required to prevent different threads from accessing shared resources simultaneously. A piece of code that accesses a shared resource—that must not be concurrently accessed by more than one thread—is called a critical section (http://en.wikipedia.org/wiki/Critical_section). To avoid race conditions, a mechanism is required at the entry and exit of the critical section. In Windows applications, critical sections are part of the WinAPI and in Android, we use mutexes from the pthread
library, which serve the same purpose.
Getting ready
Android native synchronization primitives are POSIX-based. They include thread's management functions, mutexes, conditional variables, and barriers. Take a look at the header file platforms\android-14\arch-arm\usr\include\pthread.h
in your NDK folder.
How to do it...
- Let's create an API-independent abstraction to synchronize threads:
class Mutex { public: Mutex() { #if defined...