I'm programming in C++ for Windows, and I have a HANDLE
object to a mutex defined as:
static HANDLE Instance_Mutex;
I want to check whether this HANDLE
is initialized by a mutex object or not; i.e is the code:
Instance_Mutex = CreateMutex(NULL,FALSE,NULL);
already been called.
If it is just lock it, and if not, create the mutex on this HANDLE
and then lock it. What is the appropriate way to do it then?
Does the HANDLE
object have a specific value when it has not yet been created?
If HANDLE
is a global, then it will be zero initialized (i.e. it will be nullptr
).
It would be superior style, however, to explicitly initialize it to nullptr
.
Incidentally nullptr
is also what CreateMutex
returns on failure. So this invariant (nullptr
if the HANDLE
isn't a mutex) will be maintained even then.