S
SharpCoderMP
is it possible to implement some safe way of performing two or more
instructions in a kind of "atomic" way?
consider this: we have two queues (implemented on top of an List<T>).
now we need to move an element from one queue to another, so we need to
do something like this:
queue2.Enqueue(queue1.Dequeue());
int this case we may assume that nothing can cause this to fail
resulting in loss of the object we want to move.
great, but what if we've got the reference to some object on one queue
and we want it to move from one queue to another?
then we need something more or less like this:
Item item = queue1.GetItem(someItemIndex);
if (item.DoSomething())
{
queue2.Enqueue(item);
queue1.Remove(item);
}
now as you can see for a moment we have our item on two queues at the
same time. if we reverse the order of Enqueue and Remove, for the moment
our item wont be assigned to any of the queues.
in general this would not be a huge problem... but i deal with remoting
and quite a lot of asynchronous calls... so having the same item on two
queues at the same time or not having it on any of them at all may lead
to huge troubles. any ideas how these can be avoided?
instructions in a kind of "atomic" way?
consider this: we have two queues (implemented on top of an List<T>).
now we need to move an element from one queue to another, so we need to
do something like this:
queue2.Enqueue(queue1.Dequeue());
int this case we may assume that nothing can cause this to fail
resulting in loss of the object we want to move.
great, but what if we've got the reference to some object on one queue
and we want it to move from one queue to another?
then we need something more or less like this:
Item item = queue1.GetItem(someItemIndex);
if (item.DoSomething())
{
queue2.Enqueue(item);
queue1.Remove(item);
}
now as you can see for a moment we have our item on two queues at the
same time. if we reverse the order of Enqueue and Remove, for the moment
our item wont be assigned to any of the queues.
in general this would not be a huge problem... but i deal with remoting
and quite a lot of asynchronous calls... so having the same item on two
queues at the same time or not having it on any of them at all may lead
to huge troubles. any ideas how these can be avoided?