|
Hello everyone,
I have the following scenario, and my question is whether I need to add lock or not?
In my scenario, there are two threads, one thread is accessing Dictionary instance Dic1, and the other thread is creating new Dictionary instance regularly in some interval, and the new Dictionary instance is named Dic2, and this thread will assign the instance pointed by Dic1 to Dic2, so that the new produced Dic2 could be processed by the first thread.
(Dic1 is like data consumer, and Dic2 is like data producer.)
1.
My question is whether I need lock for the operation for Dic1 of thread1, and Dic2 to Dic1 assignment operation in thread 2?
2.
Beyond using lock, are there any more efficient method? e.g. less locking? or not using lock at all?
Some pesudo code,
lock (Dic1)
{
Dic1.op1();
Dic1.op2();
Dic1.op3();
}
Dictionary Dic2 = new Dictionary();
Dic2.insert();
Dic2.insert();
Dic2.insert();
lock (Dic1)
{
Dic1 = Dic2;
}
thanks in advance,
George
|
|
|
|
|
I would use lock, if not you may be perfoming some operation with the data in Dic1 when you change it, who knows what could happen.
I think lock is fairly efficient though. Unless your operations take 5 minutes, and the second thread has to wait for the first to release the Dictionary.
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Thanks SK,
Generally, I agree with you. But what do you mean "your operations"? Could you clarify which operation in which thread do you mean please?
regards,
George
|
|
|
|
|
I meant where you did:
Dic1.op1();
Dic1.op2();
Dic1.op3();
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Thanks for your clarification, SK!
regards,
George
|
|
|
|
|
Hi George,
I would do that without any lock, but with careful programming:
1.
the dictionary producer (Thread2) does not need a lock, since Dic1=Dic2; is an
atomic operation. The statement either has been entirily executed, or it has not at all;
there is no way to have half of a reference replaced.
2.
The consumer wants a consistent dictionary, so make a local copy and use that throughout,
as in:
{
Dictionary localDic=Dic1;
localDic.op1();
localDic.op2();
localDic.op3();
}
|
|
|
|
|
So great, Luc!
1.
I have re-written the code as you suggested, here is my code, could you help to review whether it is your idea please?
{
Dictionary localDic=Dic1;
localDic.op1();
localDic.op2();
localDic.op3();
}
{
Dictionary Dic2 = new Dictionary();
Dic2.insert();
Dic2.insert();
Dic2.insert();
Dic1 = Dic2;
}
2.
Luc Pattyn wrote: Dic1=Dic2; is an
atomic operation.
Do you mean all reference assignment operation is atomic in C#? Any support documents?
regards,
George
|
|
|
|
|
Hi George,
1.
you don't need me to review a couple of lines, do you?
2.
In CLR assigning a reference is atomic (in the end what gets executed is an assembly
instruction that stores a 32-bit or 64-bit pointer value); it cannot be anything else,
what would be the purpose of half an assignment? You should be able to come to that
conclusion by reading the IL instruction set and documentation, I did that once,
but I have not done that as of late.
|
|
|
|
|
Hi Luc,
1.
Since English is not my native language, I want to make sure my understanding of your description is correct. So, reviewing the code is more about reviewing whether I understand your points.
Does my code correct?
2.
Looks great! But I see many code either using lock or using Interlocked.Exchange just to assign values for reference variable. Seems they do not know this feature.
regards,
George
|
|
|
|
|
Hi George,
1. your code is fine.
2.
you need locks or something similar when you NEED synchronisation, that is when you must
be sure about the order of things.
In your case, you don't care whether you use the old or new dictionary, the only thing
you want to avoid is using the old dictionary for half of the thread1 job and the new one
for the remainder.
If consistency is all you need, you don't need locks, just copy the reference at a well chosen
point.
|
|
|
|
|
Really great! Thanks Luc.
regards,
George
|
|
|
|
|
hi i would like to seperate my design page from validation. so i put the validation code in e.g validate.cs
design form
private void textboxInteger_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
validate.txtIntValidate(txtPhone.text)
}
in my validate class
class Validate
{
public bool txtIntValidate(string txt)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
}
BUT in the validate.cs class .. how do i declare the e..
Error 3 The name 'e' does not exist in the current context
|
|
|
|
|
Good God.
e is not present because you have not passed it through. Even if you did, this method should be static, assuming this class should exist at all.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thank for the reply.. can u pls show some example to get all verification done is the class ? or at least guide me on declaring or passing the e
thanks
|
|
|
|
|
Passing variables is a fundamental coding practice. How do you think you might want to do it?
Here's a clue: The "e" parameter was passed into the function from which you're calling your validation function.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
i want website,ask me proje and say help solution(type code).
pliz help me, i need.
lots of tnx
|
|
|
|
|
I *think* you're asking us to write a website for you, but your question makes no sense. We try hard to understand that a lot of users do not speak English, but really, if we can't understand you, we can't help you.
It looks to me like you need to buy a book and work through it, b/c you seem to be saying you have no idea how to write your website.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hello.
I have 2 questions for you
Question 1:
I got this method:
private void writeData()
{
StreamWriter writer = new StreamWriter(saveMonsterSetBase.FileName, false);
for (int i = 0; i < MonsterSetBaseDataArray.Count; i++)
{
writer.Write(MonsterSetBaseDataArray[i]);
}
writer.Close();
}
Which sort of works
It writes data from my arraylist to a file
The problem is it writes it in one line.
If my arraylist is like this:
line 1
line 2
line 3
It writes in the file like this:
line 1 line 2 line 3
How can I make a line changment?
Question 2:
I have a string that looks like this:
string testString = "1 10";
and I want to replace 1 and 10 with something
I replace it like this:
testString.Replace("1", "One").Replace("10", "Ten");
But the problem is that the output for 10 comes out as OneTen
Is there any way to replace 1 as just One and 10 as just Ten?
|
|
|
|
|
Soloution found:
1:
[writer.WriteLine(MonsterSetBaseDataArray);
2:
testString.Replace("10", "Ten").Replace("1", "One");
|
|
|
|
|
Hello everyone,
Using IFilter, I am trying to read name of the custom value-type property of a document. I got the Id and value but couldn't read the name.
So, can you please give me any suggestion or solution to solve my problem.
Thank you.
|
|
|
|
|
I have a Visual Studio Setup and Deployment project that associates a file type with my app. I have setup the file type in my Setup and Deployment project and ran the installer, however in windows explorer when you right click a file of that particular type and select "open with" it will show just the app's icon but no text is displayed next to the icon (usually the name of the application is shown here). Does anyone know what I'm doing wrong?
Note: if you double click the file it will successfuly launch the app correclty. It's just the application name in the windows explorer "open with" menu that doesn't show.
thanks
|
|
|
|
|
Did you set the application title and description in the Assembly Properties of the VS project? AFAIK, Windows Explorer pulls the display name of the application from that information.
|
|
|
|
|
Dear Friends,
I the project i am working on, i've a requirement of some very simple work related to active directory. Actually, i've to just query some data.
Now i am searching for some kind of article from which i can quickly learn only the basics of Active Directory, its installation and some basic programming of AD in C#.
If you are aware of any such resource(s) which can help me in quickly done my job then please tell me.
Sometimes google is not of much help and experience of friends is more useful.
Imtiaz
|
|
|
|
|
Here[^] Is a few.
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|