wait for thread to terminate