For Mac users "pthread_mutex_lock(&lock)" is slow on MACs compared to Linux: use instead: "while (pthread_mutex_trylock(&lock) > 0) {} ;" which implements a spin lock