|
Sample:
Here I have a collection class that I want to be thread safe for reading from and manipulation (Not also in the case of iterations). It is assumed any number of threads may enter any of the methods or indexers at the same time. I think for reading from indexers there is no need for a lock, even if there is a chance for more than one thread to enter the indexers, and/or at the same time to one of the Add, Remove, Clear methods. Is it correct? (sorry I don't know how to format code snippets in the post)
internal class MyThreadSafeCollection : System.Collections.Specialized.NameObjectCollectionBase
{
const int readLockTimeout = 100;
const int writeLockTImeout = 100;
private ReaderWriterLock rwLock = new ReaderWriterLock();
public MyThreadSafeCollection()
{
}
public object this[int index]
{
get
{
rwLock.AcquireReaderLock(readLockTimeout); // IS THIS REQUIRED ??
try
{
return BaseGet(index);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
}
public SocketClientData this[string id]
{
get
{
rwLock.AcquireReaderLock(readLockTimeout); // IS THIS REQUIRED ??
try
{
return BaseGet(id);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
}
public void Add(string id, SocketClientData client)
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseAdd(id, client);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public void Remove(string id)
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseRemove(id);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public void Clear()
{
rwLock.AcquireWriterLock(writeLockTImeout); // I Know it is required. (isn't it?)
try
{
BaseClear();
}
finally
{
rwLock.ReleaseWriterLock();
}
}
---
"Art happens when you least expect it."
|
|
|
|
|
OK, AFAIK the general answer is yes, you need all the locks:
1)
for operations that modify the data (add, remove, clear) if more than one thread were
to perform a modification at the same time, the result would be unpredictable without lock.
2)
for operations that just read the data, if another thread were to write at the same time,
the results again COULD be unpredictable (it depends on how the data actually is structured),
so you should acquire a reader lock (which is less demanding than a writer lock, it does not block other readers on the same object).
3)
of course there is an overall concern as to where the lock needs to be implemented.
My answer typically is at the highest possible level, i.e. the level where your
functional primitives are located. Example: if you use an indexor into a collection,
what is the meaning of the index if the collection could change in the mean time ?
void sumAll(myCollection) {
int sum=0;
int n=myCollection.Count;
for (int i=0; i<n; i++) sum=sum+myCollection[i];
return sum;
}
in the above snippet, seems to me the entire for loop needs a lock, otherwise how
can we be sure we get the collection items we intended, since without an overall lock
the collection (and hence the index-object relations) may change (probably not so
if lookup by key). In the example, the items summed may not be the ones that were in
the collection at the time the count was obtained !
Once you do use an overall lock, you may not want a low-level lock
anymore (and that is, I guess, why the NET Collections dont have a lock built-in).
So in conclusion:
- there has to be a lock on all operations
- positioning the lock code is a design issue:
too low ==> may produce wrong results
too high ==> may be counterproductive (multithreading not working anymore),
and possibly deadlocks
as for performance, locks at higher level are better (fewer acq/rel).
- All this makes it difficult, if not impossible, to build a really useful thread-safe
collection, independent of the application itself.
Hope this helps.
Luc Pattyn
|
|
|
|
|
Thank you very very much buddy, Yes that really helped and clarified a lot for me
I wish I can help u back some day.
---
"Art happens when you least expect it."
|
|
|
|
|
Hello every one. i have develope a chat software in C# and online help for it but i dont know the way how to access the main page of that online help from the help menu button. Is there any one who can help me in this regard??????
|
|
|
|
|
Process.Start will launch the doc with the default viewer. IE can be embedded in your app, if you prefer.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I've finshed my project but I could not run my program on other computer.
I tried update .NET framework 2.0 but it still not work.
How can I make sure my program will run on every PC (XP Sp2 + .NET 2.0)
|
|
|
|
|
If you've installed .NET 2.0, then you must be using another component that you need to install. What's the error - if you have .NET installed, it should run enough to tell you what it needs.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
What you are talking about is "deploying applications".
You find (almost) all you need here[^].
SkyWalker
|
|
|
|
|
Even when I use deploying, it is error. Pop up window shown "application error and need to close". That's all. It does not tell me anything.
|
|
|
|
|
Something must go wrong.
Try a debug version of your application on the computer where it doesn't work. You need to get more information on what's going on.
SkyWalker
|
|
|
|
|
I found out that I included some library which is not common amoung XP SP2.
After removed this outeverything work fine.
Thank you very much
|
|
|
|
|
Good
SkyWalker
|
|
|
|
|
Hi,
I have function that is in my class file (ojblib.c) this function is returning a single value based from my select statement. Could someone please help me on how to return 2 to 3 value at the same. Is dataset correct or is there any other way to return a value?
Name.Text = GetValue("Select username,tel,addres from employee where id=123");<br />
<br />
function string GetValue(string TxtSel)<br />
{<br />
string svalue="";<br />
objcon.Open()<br />
objcmd = new sqlcommand(TxtSel,objcon);<br />
svalue = objcmd.executescalar().ToString();<br />
objcon.close()<br />
return svalue.ToString();<br />
}
Thanks
/Dabuskol
Dabsukol
|
|
|
|
|
you can pass ref(ByRef in VB) parameters :
<br />
function string GetValue(string TxtSel,ref string Return1, ref string Return2)<br />
|
|
|
|
|
You can also use the out keyword. In contrast to the ref keyword, it doesn't require the so marked variable to be initialized before the method call.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Actually, the big difference is your function cannot return before assigning a value to an out parameter.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Thanks for the addition Didn't knew this as I've never used out parameters myself.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
you can also define a struct that contains the values and return that
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
... we could even generalize to a type ...
SkyWalker
|
|
|
|
|
You could return an object []
SkyWalker
|
|
|
|
|
Yes, you can return a data set. You can also return a data table, an array, a list or a collection.
A lot of people have suggested using ref or out , but I would advice against those in this case. They are more funcional than object oriented, and are mostly used for interfacing with components written in other languages and for perfomance tweaking. It's for example used in the Int32.TryParse method to avoid creating an object as return value, but you are creating so many objects in you function anyway that there is no reason for you to avoid creating another one.
---
Year happy = new Year(2007);
|
|
|
|
|
Guys, please try to remember what we learnt back at school.
A function should return only one parameter. Like Christian wisely said, returning a structure is the only "nice" way of doing it.
Everything that would disguise a function so that it can return more than one value, by using ref variable for instance, is making in fact a procedure. if so then please, make a procedure
|
|
|
|
|
I have done a c# windows application, It should be automatically adaptable for all the screen resolution like 1024x768 or 800x600 and (ect) .how to do this?
we are in illusionary life
|
|
|
|
|
Take a look at the Control.Dock and Control.Anchor properties. To start your form in maximized mode set the Form.WindowState property.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|