Mutual exclusion for read/write - but no sync/lock for concurrent reads

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: [ckrm-tech] Circular Locking Dependency Chain detected in containers code
    ... holding container mutex. ... should be able to rmdir when a container is getting mounted. ... possible circular locking dependency detected] ... rmdir/4321 is trying to acquire lock: ...
    (Linux-Kernel)
  • Re: Locking an STL-like container
    ... For locking the container while insertand erasemethods are ... as you said they do not support read-write locks. ... Your reader thread must take a snapshot then release the lock. ... you will have a class that "has" your container and controls locking. ...
    (comp.programming.threads)
  • Circular Locking Dependency Chain detected in containers code
    ... should be able to rmdir when a container is getting mounted. ... just do a rmdir inside the container. ... possible circular locking dependency detected] ... rmdir/4321 is trying to acquire lock: ...
    (Linux-Kernel)
  • Re: Containers falling off freight trains
    ... method of ensuring that all the twistlocks are in place before the train ... Is it possible that the 'socket' on a container ... They are made from hardened high tensile steel whereas the ... If you do a Google search on "container twist lock" and then click on ...
    (uk.railway)
  • Re: bag smashing
    ... box has handles, hinges,and hasp and lock on it. ... steel corners and banded the perimeter of the box. ... I've used this container for several years and no problem, ...
    (rec.travel.air)