I'm not providing full listing as below code is enough for those who familar with disruptor.
The question is if calling Next
and Publish
methods is thread-safe. Between examples below what would be the correct one? Note that Attach
can be called from different threads at the same time. And I have multiple consumers.
Example1. Lock everything:
private object attachLock = new object();
// can be called from parallel threads
public void Attach(OrdersExecutor oe)
{
lock (attachLock)
{
long sequenceNo = ringBuffer.Next();
ringBuffer[sequenceNo].Value = oe;
ringBuffer.Publish(sequenceNo);
}
}
Example2. Lock Next:
private object attachLock = new object();
// can be called from parallel threads
public void Attach(OrdersExecutor oe)
{
long sequenceNo;
lock (attachLock)
{
sequenceNo = ringBuffer.Next();
}
ringBuffer[sequenceNo].Value = oe;
ringBuffer.Publish(sequenceNo);
}
Example3. No lock
private object attachLock = new object();
// can be called from parallel threads
public void Attach(OrdersExecutor oe)
{
long sequenceNo = ringBuffer.Next();
ringBuffer[sequenceNo].Value = oe;
ringBuffer.Publish(sequenceNo);
}