Mutual exclusion for read/write - but no sync/lock for concurrent reads
- From: illegal.prime@xxxxxxxxx
- Date: 14 Mar 2007 14:00:45 -0700
So I have a container of objects that I don't want to iterate across
when I'm modifying it. I.E. I lock on adds and deletes to the
container - so that my traversals of it don't result in concurrency
issues.
However, what do I need to do to allow multiple threads to traverse
the container without synchronization/mutual-exclusion - but ensure
that synchronization/mutual-exclusion is there when the container is
trying to be both changed and traversed?
My guess would be to use semaphores - but the only synchronization
mechanism I've used in C# is the lock statement. What do I need in
this scenario?
Here is some pseudo code:
class Foo
{
private List<Bar> mMyContainer = new List<Bar>();
public void DoSomething (int i)
{
lock (mMyContainer)
{
((Bar)mMyContainer[i]).DoSomething();
}
}
public void AddElement(Bar bar)
{
lock (mMyContainer)
{
mMyContainer.Add(bar);
}
}
}
Novice
.
- Follow-Ups:
- Re: Mutual exclusion for read/write - but no sync/lock for concurrent reads
- From: Jon Skeet [C# MVP]
- Re: Mutual exclusion for read/write - but no sync/lock for concurrent reads
- Prev by Date: Re: ArrayLists and AddRange
- Next by Date: Re: Very strange "input string not in correct format" bug
- Previous by thread: Re: Creating Event to Monitor a Value
- Next by thread: Re: Mutual exclusion for read/write - but no sync/lock for concurrent reads
- Index(es):
Relevant Pages
|