Wait for thread to exit cleanly