I am following the examples from "Android NDK Game Development Cookbook" for creating a cross-platform thread wrapper for use in my own Android NDK game engine. In the example's Thread class, at a certain point, a check is done to see if a thread handle corresponds to the current thread. This is done as follows:
if ( GetCurrentThread() != FThreadHandle )
The FThreadHandle handle was assigned using the follwing call earlier on:
#if defined(_WIN32)
FThreadHandle = ( uintptr_t )_beginthreadex( NULL, 0, &ThreadStaticEntryPoint, ThreadParam, 0, &ThreadID );
#else
pthread_create( &FThreadHandle, NULL, ThreadStaticEntryPoint, ThreadParam );
#endif
And the getCurrentThread function is defined as follows:
native_thread_handle_t iThread::GetCurrentThread()
{
#if defined( _WIN32)
return GetCurrentThreadId();
#elif defined( ANDROID )
return gettid();
#else
return pthread_self();
#endif
}
My question is: Is this function implementation correct? I have 3 major concerns about this implementation:
- I would expect that for _WIN32, the GetCurrentThread function would be used instead of GetCurrentThreadId, since we want the handle of the thread and not its id.
- Furthermore, why is gettid() used for android instead of pthread_self()? Wouldn't I get the wrong id using gettid (since this is a system wide thread id)?
- And lastly, is it legal to just use the != operator to compare pthread ids? Shouldn't pthread_equal be used instead?