Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having multiple arrays share a syncroot 2

Status
Not open for further replies.

robotduck

Programmer
Aug 8, 2006
14
GB
I'd like to have three arrays all share the same syncroot, so that in my multi-threaded application, if any thread gets a lock on any one of the 3 arrays, no other thread can modify any of the 3 arrays until the lock is released. Is this possible? is it a really bad idea?

I have tried setting the syncroot of the arrays (which failed, because it's read-only).

any ideas welcomed!

thanks,
Ben
 
Why not synchronize them yourself? Create your own sync object and use the same one when accessing your arrays?

<bb />
 
If these arrays have to be kept in sync, I would embed your arrays in their own class with it's own getter/setter properties that external callers would use. This class would have it's own internal syncroot object.

This way the actual implementation of how your data is stored is hidden from the callers, and usage becomes transparent to them.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
If you're using .net v2.0, you might also want to look at the System.Transaction namespace, so that your changes to these arrays can be written to atomically.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I have a similar problem.

I have some List<MyClass> arrays in memory and I think I have to lock them to get rid of an error, but do I have to lock just for insert and flush, or do I also have to lock on read?

I used this code to help me:

My read method goes something like this:

Code:
return _changeList.Exists(delegate(ChangeBE e)
{
    return (
        (e.UserName != userName) &&                                          
        (e.ListType.Substring(0, 2) == myListType) &&
        (e.Timestamp > lastChecked)
    );
});


[elephant2]
graabein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top