ensure Thread::flags is always updated atomically

Since this field is sometimes updated from other threads, it is
essential that we always update it atomically.