Adding a pthread_join() here to free the resources used by the new thread will solve the race condition issue because this thread (zm's main thread) will block until the reopen ffmpeg thread is done.