Removing race condition using pthreads mutex