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 / Performance / August 2003

Tip: Looking for answers? Try searching our database.

Threading and SyncLock Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Craig Santoli - 21 Aug 2003 21:35 GMT
Let's say I have the following simple class in VB.Net...

Public Class MyClass

   Private m_strSyncLockSave as string = "Save"

   Private Sub Save

       SyncLock  m_strSyncLockSave
           ...some code here...
       End SyncLock

   End Sub

End Class

Now let's say I create 2 instances of this class; MyClass1 and MyClass2.

Next, let's say I have 4 threads created...
Threads 1 and 2 call MyClass1.Save.
Threads 3 and 4 call MyClass2.Save.

All 4 threads call their respective procedures at the same time.

I understand that both calls to MyClass1.Save will not execute at the same
time; the one that starts second will wait for the first to complete.

But my question is... Is the Save sub locked across MyClass1 and MyClass2,
or can MyClass1.Save execute at the same time as MyClass2.Save?

Thanks,
Craig Santoli
Daniel Moth - 21 Aug 2003 21:47 GMT
Hi

The answer is that not all 4 threads will be blocked (the lock as you coded
it is per object)... If you did want the lock to be cross-object you could
try locking on the object's type or a shared/static object ...

Cheers
Daniel

> Let's say I have the following simple class in VB.Net...
>
[quoted text clipped - 28 lines]
> Thanks,
> Craig Santoli
David Browne - 21 Aug 2003 22:45 GMT
> Hi
>
[quoted text clipped - 4 lines]
> Cheers
> Daniel

Unfortunately it's not quite that simple.
Since m_strSyncLockSave is set to the literal "Save", it will refer to an
intened string, and all instances will actually point to the same object.

To fix this always use a "new Object" for syncronization instead of a
string.

Like this:

Public Class StringLock
 Private m_SyncLockSave As new object

Here's a sample.

David

Public Class StringLock

 Private m_strSyncLockSave As String = "Save"

 Public Sub Save()

   SyncLock m_strSyncLockSave
     System.Threading.Thread.Sleep(1000 * 5)
     Console.WriteLine(AppDomain.GetCurrentThreadId & " " & Now)
   End SyncLock

 End Sub

End Class

Module Module1

 Sub Main()
   Dim s1 As New StringLock
   Dim s2 As New StringLock
   Dim t1 As New Threading.Thread(AddressOf s1.Save)
   Dim t2 As New Threading.Thread(AddressOf s2.Save)
   t1.Start()
   t2.Start()
   t1.Join()
   t2.Join()

 End Sub

End Module
Daniel Moth - 21 Aug 2003 22:45 GMT
Interesting...

> > Hi
> >
[quoted text clipped - 52 lines]
>
> End Module
Brian Matzon - 22 Aug 2003 06:55 GMT
> Public Class StringLock
>
>   Private m_strSyncLockSave As String = "Save"
[SNIP]
> End Class
> Module Module1
[quoted text clipped - 12 lines]
>
> End Module

Am I missing something here?
The former example was flawed because it synchronized on a string, which
might have been interned. The example that is then posted, does the
*exact* same thing, albeit in to different instances of the same class.

I am no expert in interning, but afaik, all literal strings are intered
so in the above example "Save" will also refer to the same object!

Wouldn't it just be simpler to do a:
Private m_strSyncLockSave As new Object()

at least, thats what I usually do...

/me is confused

/Matzon
David Browne - 22 Aug 2003 14:33 GMT
> > Public Class StringLock
> >
[quoted text clipped - 29 lines]
>
> at least, thats what I usually do...

The exaple was provided to demonstrate that two instances syncronizing on a
string member variable which is initialized to a literal would share the
same Monitor -- that of the interned string.

And the recommended fix was to use a new Object() to syncronize, like you
suggested.

David

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.