That article link:

http://msdn.microsoft.com/msdnmag/issues/03/01/NET/default.aspx

Fred

-----Original Message-----
From: Andrew Gayter [mailto:[EMAIL PROTECTED]
Sent: Tuesday, October 12, 2004 9:52 AM
To: [EMAIL PROTECTED]
Subject: **Spam** Re: [ADVANCED-DOTNET] Multithreading question

It could be easier to look at the following code first

class myclass
{
  private ArrayList myList = new ArrayList();

  void func1()
  {
        // thread placed into a wait state if it cannot acquire the lock
        System.Threading.Monitor.Enter(myList)
        Try
        {
                foreach(Item myItem in myList)
                ...
        }
        Finally
        {
                System.Monitor.Exit( myList );
        }
  }

The lock(...) command simply expands out to what you see above when
compiled. The finally block ensures we don't end up in a deadlock situation
i.e. not releasing the lock should a return/exception get thrown.

To perform locking you must supply a 'reference' type to the monitor/lock
method. References types, as opposed to value types have a header, which
contains a syncblock which is used for the synchronization.

Take a look at
http://msdn.microsoft.com/msdnmags/issues/03/01/net which explains the
locking mechanisms employed in .NET

btw: you may want to take a look at the ReaderWriterLock which performs
better than lock/monitor methods.

A


-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Steve Johnson
Sent: 12 October 2004 14:38
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Multithreading question

When you "lock" an object, there's no protection against other threads
accessing that object.  The system simply enforces the constraint that
only 1 thread that "asks" for the lock will be granted that lock at a
time.  You need to ensure that every place in your code that accesses
your array list asks for the same lock first.  The object referenced
in your lock statement doesn't even have to be the object you're
trying to protect.  As long as every place in the code that accesses
the array list uses the same lock, you're fine.

class myclass
{
  private ArrayList myList = new ArrayList();

  void func1()
  {
    // will block if any other thread holds a lock on myList
    lock(myList)
    {
      foreach(Item myItem in myList)
        ...
    }
  }

  void func2()
  {
    // not protected.  Even if another thread holds a lock, access is not
    // prevented here.
    myList.Add(anotherItem);
  }
}


To fix this sample, you'd wrap the myList.Add() statement with
lock(myList).  The synchronization system here provides protection
against concurrency only when everybody plays by the same rules.


--
Steve Johnson

On Tue, 12 Oct 2004 21:07:46 +1300, [EMAIL PROTECTED] <[EMAIL PROTECTED]>
wrote:
> I am having trouble understanding .net and c#'s lock command
>
> What happens if a lock is called on a object that is already being used
> by another process.
>
> I am getting weird behavior in dotnet remoting to do with threads.
>
> If i put a lock on all methods in there, i get a deadlock which i would
> expect, but when i only put a lock on the only method that enumerates
> through a arraylist i get a enumeration violation because of
> modification to the array during enumeration.  I dont see how this is
> possible if i put a lock on the object that contains the arraylist while
> i enumerate through it. ?!?!?!?!
>
> I am thinking if another thread already is already in that object both
> may execute at the same time regardsless of the lock block.
>
> Can anyone confirm or deny my theory and give me some advice on what
> might be going on.

===================================
This list is hosted by DevelopMentor.  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to