Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2004

Tip: Looking for answers? Try searching our database.

When to use Synchronized method and When to use lock?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lei Jiang - 28 Jul 2004 06:07 GMT
To prevent concurrent access to a data structure, one way is to use its
Synchronized method, another way is to use unlock keyword in C#. I'd like to
know how to choose from them. For example :

class MyClass
{
   ArrayList list = new ArrayList();

   void Method(object newObj)
   {
       // One way :
       ArrayList l = ArrayList.Synchronized(list);
       ....

      // Another way :
      lock(this) { ... }
   }
}

What's the difference between this two methods? Thanks!
Jon Skeet [C# MVP] - 28 Jul 2004 06:54 GMT
> To prevent concurrent access to a data structure, one way is to use its
> Synchronized method, another way is to use unlock keyword in C#. I'd like to
[quoted text clipped - 16 lines]
>
> What's the difference between this two methods? Thanks!

I would personally recommend locking, but not on "this". Using the
synchronized wrappers only makes each operation synchronized - you
still have to lock (on the SyncRoot) for any sequence of operations you
want to be atomic (such as iterating through the list).

See
http://www.pobox.com/~skeet/csharp/multithreading.html#lock.choice for
more about choosing what to lock on.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Lei Jiang - 28 Jul 2004 09:28 GMT
Thanks!

> > To prevent concurrent access to a data structure, one way is to use its
> > Synchronized method, another way is to use unlock keyword in C#. I'd like to
[quoted text clipped - 25 lines]
> http://www.pobox.com/~skeet/csharp/multithreading.html#lock.choice for
> more about choosing what to lock on.
Christoph Nahr - 28 Jul 2004 07:48 GMT
>To prevent concurrent access to a data structure, one way is to use its
>Synchronized method, another way is to use unlock keyword in C#. I'd like to
>know how to choose from them.

Easy.  Don't use Synchronized, ever.  Always use lock (list.SyncRoot).
Signature

http://www.kynosarges.de

Lei Jiang - 28 Jul 2004 09:29 GMT
Thanks. But why?

> >To prevent concurrent access to a data structure, one way is to use its
> >Synchronized method, another way is to use unlock keyword in C#. I'd like to
> >know how to choose from them.
>
> Easy.  Don't use Synchronized, ever.  Always use lock (list.SyncRoot).
Christoph Nahr - 28 Jul 2004 12:35 GMT
>Thanks. But why?

Because Synchronized only gives the illusion of thread safety.
For example, the following code is NOT thread-safe:

ArrayList list = new ArrayList();
ArrayList syncList = ArrayList.Synchronized(list);
...
int index = syncList.IndexOf(foo);
if (index >= 0) syncList[index] = bar;

Between calling IndexOf and changing the object at index, another
thread might jump in and change the contents of syncList.  This is
possible because Synchronized protects only individual operations, not
sequences of two or more operations.

By contrast, the following code IS thread-safe (provided all other
threads also call lock (list.SyncRoot) before accessing the list):

lock (list.SyncRoot) {
   int index = list.IndexOf(foo);
   if (index >= 0) list[index] = bar;
}
Signature

http://www.kynosarges.de

Lei Jiang - 29 Jul 2004 02:25 GMT
Thanks. I see .

> >Thanks. But why?
>
[quoted text clipped - 19 lines]
>     if (index >= 0) list[index] = bar;
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.