473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best way for "atomic" like code parts execution?

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?
Oct 30 '06 #1
5 1684
Is using MSMQ a possible design solution for you? If so, you can
create transactional queues using the System.Messaging framework.

http://windowssdk.msdn.microsoft.com.../8bzbb471.aspx

http://windowssdk.msdn.microsoft.com.../429s2d9f.aspx

Bill

Oct 30 '06 #2
Well, allowing for suitable compensator "catch" blocks, the only thing you
really neeed to watch out for is competing threads reading the queues while
you are mid-change.

However, if your competing threads are capable of doing this, you already
have major problems, as your code is inherently not thread safe.

The answer here is to lock (synchroize) /both/ containers prior to working
on them. All other threads (including remoting) should be doing likewise,
annd then there is no problem. Of course, you need to agree on the lock
order to avoid deadlocks...

Marc
Oct 30 '06 #3
thanks... um... lock... temporary stupidity got me ;P
lock should work fine.

but you suggest to lock containers - wouldn't it be better to only lock
the item we're currently dealing with?
Oct 30 '06 #4
No, as you don't want to add or remove any items for any queue outside of a
lock (if multiple threads).
So, if you can do it, try to use 1 lock for both queues as their syncLock.
Then if you need a multi-step operation, just take the lock and move stuff
around and exit lock.

private void MoveStuff()
{
lock(syncRoot)
{
// Move stuff around.
}
}

--
William Stacey [C# MVP]

"SharpCoderMP" <cs*******@interia.pl.NFSPMwrote in message
news:uS**************@TK2MSFTNGP05.phx.gbl...
| thanks... um... lock... temporary stupidity got me ;P
| lock should work fine.
|
| but you suggest to lock containers - wouldn't it be better to only lock
| the item we're currently dealing with?
Oct 30 '06 #5
Personally I'd try and see if I could use separate locks for the
containers, so that in normal operation they can be used independently,
and only take out both locks when transferring... of course, this makes
it more complex and (if not done correctly) prone to deadlock.

Of course, if this (separate access when not transferring) isn't an
issue, or you only have a few threads (so no need to support larger
parallelism) stick with one lock for simplicity.

Marc

Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5263
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
1
1818
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
4
1970
by: Robin Tucker | last post by:
Hi, I'm currently implementing a database with a tree structure in a table. The nodes in the tree are stored as records with a column called "Parent". The root of the tree has a "NULL" parent....
388
21396
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
14
11939
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
10
2874
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any...
28
7334
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
94
30186
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
350
11457
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.