Improve efficiency and fairness when combining temporally close events
See the question and my original answer on StackOverflowNot sure it does what you want, but here is my proposition. It basically hands off any B message to A whenever possible, and checks the message has been sent after all:
class MessageWrapper
{
object gate = new object();
int? pendingB;
public Message WrapA(int a, int millisecondsTimeout)
{
int? b;
lock (gate)
{
b = pendingB;
pendingB = null;
Thread.Sleep(1); // yield. 1 seems the best value after some testing
}
return new Message(a, b);
}
public Message WrapB(int b, int millisecondsTimeout)
{
int? bb = b;
lock (gate)
{
if (pendingB == null)
{
pendingB = b;
bb = null;
}
}
Thread.Sleep(3);
if (bb == null)
{
lock (gate)
{
if (pendingB != null)
{
bb = pendingB;
pendingB = null;
}
}
}
return new Message(null, bb);
}
}