Use the pthread_once(_:_:)
function to ensure that initialization is called before a mutex is used.
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t mutex;
void init() {
pthread_mutex_init(&mutex, NULL);
}
void performWork() {
pthread_once(&once, init); // Correct