Waiting for monitor thread to exit before killing shared memory