Game Development Reference
In-Depth Information
How it works...
To demonstrate the usage of the implemented thread class, we deine a new thread, which
prints out a message every second:
class TestThread: public iThread
{
public:
virtual void Run()
{
printf("Test\n");
Sleep(1000);
}
};
void Test()
{
TestThread* Thread = new TestThread();
Thread->Start();
while (true) {}
}
Now, the implementation of a simple multithreaded application in C++ is not much harder
than in Java.
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 ile platforms\android-14\arch-arm\usr\include\pthread.h in your
NDK folder.
 
Search WWH ::




Custom Search