add PTHREAD_MUTEX_RECURSIVE to prevent deadlock