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.
Merge pull request #329 from dicej/atomic-flags

ensure Thread::flags is always updated atomically