public M TryConsume() { MsgHolder observed, newValue = null; do { observed = msgHolder; if (observed != null && observed.lives > 0) { newValue = new MsgHolder(observed.msg, observed.lives - 1); } else { return(null); } } while (Interlocked.CompareExchange(ref msgHolder, newValue, observed) != observed); return(newValue.msg); }
public void Publish(M m, int lvs) { msgHolder = new MsgHolder(m, lvs); }