There are many questions on stackoverflow about if a pthread mutex can be shared between processes, but I found no questions/answers regarding initialization of the shared mutex.
As far as I understand, the common way of using a process-shared mutex is the following: allocate a block of shared memory, initialize a pthread mutex on the shared memory block, use it.
In case of shared memory creation, it is handled by OS if multiple processes try to allocate a shared memory block with the same key ID. OK, but what I don't understand is how can I initialize a mutex on the shared memory block safely?
Am I right that the pthread_mutex_init doesn't provide any safe approach to initialize the pthread_mutex_t simultaneously from different processes? If yes, how can I provide exclusive access for processes to initialize a shared "mutual exclusion"? And how can I make sure if another process initialized the mutex successfully or not?
The second question relates to a case when a process blocking a mutex crashes. OK, there is a robust mutex which handles such cases and returns a corresponding error code. What about the shared memory block? It seems like a process should take care about if it is the last process which uses the shared memory to destroy it.