473,687 Members | 3,552 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.DoSomethi ng())
{
queue2.Enqueue( item);
queue1.Remove(i tem);
}

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 1696
Is using MSMQ a possible design solution for you? If so, you can
create transactional queues using the System.Messagin g 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]

"SharpCoder MP" <cs*******@inte ria.pl.NFSPMwro te in message
news:uS******** ******@TK2MSFTN GP05.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
5273
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 triggered by a comment on the Python Cookbook site, which basically said that it was OK to do tss = {} ... id = thread.get_ident() tss = {}
1
1830
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 put in file at a time or the string can appear in parts.String lenfth is no more than 50 chars. I have 2 processes on different solaris boxes (NFS) running in parallel.one process writing lines to the file and other process reading lines from...
4
1983
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. The path to each node is stored in the column "Path" and is of the form "\000001\000002\000003\" etc. The latter enabling me to fetch subtrees using the "LIKE" predicate. I also have created the relation "ID" <-> "ID_Parent, effectively the...
388
21691
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
14
11966
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 can be accessed as an atomic entity, even in the presence of asynchronous interrupts." Does this mean that the use of "volatile" in the above declaration is redundant? (It sure sounds that way to me.)
10
2893
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 other way to get similar effect for Int64 type? Another question less urging question is, why long variables can't be used as volatile? I understand that in 32-bit arch. 64-bit operations are not atomic, but seems that there could be locks or so...
28
7383
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 "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
94
30295
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
350
11731
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 one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
8528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6453
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5807
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4321
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2215
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.