Yes, but it may not be used.
I assume that you want to get the value if and only if it is "dirty" (since it is cleared with each extraction, so I do not see the value in the reverse order). Therefore, you must:
if(buff.Dirty) { T val = buff.GetValue(); //operations on val. }
However, if another thread calls GetValue() at the same time, then Dirty now false.
Therefore, its use is safe for only one reader thread (in this case, several writer threads are fine, since they only change Dirty in the opposite direction).
If you can have multiple readers, consider adding something like:
public bool GetIfDirty(out T value) { lock (_locker) { if(!_dirty) { value = default(T); return false; } _dirty = false; value = _value; return true; } }
Then you can both check Dirty and get the value, if you want, in the same stream operation.
source share