How will locking behave in .net?

abstract class Foo
{
    private List<Object> container;
    private bool update;

    Foo Foo()
    {
        container = new List<object>();
        update = false;
    }

    public abstract Bar CreateBar();

    public void BeginUpdate()
    {
        if (!update)
        {
            Thread update_thread = new Thread(new ThreadStart(Update));
            update_thread.Start();
        }
    }

    private void Update()
    {
        update = true;
        while (update)
        {
            lock (container)
            {
                if (...)
                    container.Add(this.CreateBar());
                else
                    container.Remove(...);
            }

            Thread.Sleep(1337);
        }
    }

    public void EndUpdate()
    {
        update = false;
    }

    public List<Object> Objects
    {
        get
        {
            lock (container)
            {
                return this.container;
            }
        }
    }
}

When something outside of Foo calls the Foo's Object accessor like,

List<Objects> objects = foo_instance.Objects;
foreach (Object o in objects)
{
    Thread.Sleep(31173);
}

How will the locking occur? Will the thread running Update() have to wait until the above foreach is done processing objects list? I would like that these two would work simultaneously, is the only solution to make a deep copy of objects?

Answers


Your code does not do what you think it does. This method

public List<Object> Objects
{
    get
    {
        lock (container)
        {
            return this.container;
        }
    }
}

Does not hold the lock after it has returned the value. So your loop is not locked.

You cannot return the container instance from the clas


Several problems with this code:

  1. You do not start the thread
  2. You can have a race-condition (might not be applicable to your program) where multiple threads call BeginUpdate, both sees that update is false, and both start a thread, now you have two threads running, possibly interfering with each other, since you have a field member with the shared data
  3. What is the point of locking in the property? You don't need it. Note that the container you return from the property will at times be in a state of flux due to the inner update. This means that outside code that calls the property will have to lock the container itself before accessing its contents. You do not need a lock to return the container reference though.
  4. Race condition if you call EndUpdate and then BeginUpdate right after each other, the old thread might not have had a chance to exit yet

I'd say that before you start worrying about what to lock and when, you should fix the code so that it doesn't have lots of other thread-related problems.


Need Your Help

Responsive layout constraining horizontal display in Lightbox2 overlay

jquery css lightbox lightbox2

I use Lightbox2 on several sites for "galleries", and never had a problem. I am now attempting to use it with a Simple Responsive template (or on Github), and running into a problem:

Call back function with delay using jquery

jquery html

Hi I use jquery call back function to removeClass, Now I want to attach delay with this function. My function is