473,770 Members | 1,785 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 1700
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
5276
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
1836
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
1986
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
21923
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
11981
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
2900
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
7411
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
30350
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
11879
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
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6710
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.