473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

synclock performance

I would like to know why locking on a new object takes significantly
longer than locking on a strongly typed object. All of the posts I've
seen say that it is best to lock on a "new object", but it takes more
than 3x longer than locking on anything other than object. Here's a
test case:

Module Module1

Sub Main()

Dim T As clsTest
Dim i As Integer
Dim D As Date
Const MAX = 10000000
Dim LL As New Object
Dim DTest As Date
Dim T2 As New clsDummy
T = New clsTest
D = Now
For i = 1 To MAX
T.Index += 1
Next
Console.WriteLine("No lock: " & Now.Ticks - D.Ticks)

T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on owner: " & Now.Ticks - D.Ticks)

T = New clsTest
D = Now
For i = 1 To MAX
SyncLock LL
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on new object: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T2
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on Dummy object: " & Now.Ticks -
D.Ticks)

Console.ReadLine()
End Sub

Class clsDummy

End Class

Class clsTest
Dim m_Index As Integer
Public Property Index() As Integer
Get
Return m_Index
End Get
Set(ByVal Value As Integer)
m_Index += 1
End Set
End Property
End Class

thanks,
Keith
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 22 '05 #1
3 2168
Keith,
I would like to know why locking on a new object takes significantly
longer than locking on a strongly typed object.
Because on each iteration there's a call to

Microsoft.VisualBasic.CompilerServices.FlowControl .CheckForSyncLockOnValueType()

which throws an exception if you're trying to lock on a boxed value
type, because that isn't allowed in VB.

All of the posts I've
seen say that it is best to lock on a "new object", but it takes more
than 3x longer than locking on anything other than object.


I bet all those posts used it in more realistic situations though. For
a one time lock, the perf difference hardly matters. I hope you don't
have real code that looks like what you posted.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #2
Mattias,

Obviously this was just a test case to demonstrate the differences,
and not real code. In light of the performance difference, doesn't it
make more sense to use an empty custom class instead of a generic
object?

Keith
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<#$**************@TK2MSFTNGP09.phx.gbl>...
Keith,
I would like to know why locking on a new object takes significantly
longer than locking on a strongly typed object.


Because on each iteration there's a call to

Microsoft.VisualBasic.CompilerServices.FlowControl .CheckForSyncLockOnValueType()

which throws an exception if you're trying to lock on a boxed value
type, because that isn't allowed in VB.

All of the posts I've
seen say that it is best to lock on a "new object", but it takes more
than 3x longer than locking on anything other than object.


I bet all those posts used it in more realistic situations though. For
a one time lock, the perf difference hardly matters. I hope you don't
have real code that looks like what you posted.

Mattias

Nov 22 '05 #3
Keith,
In light of the performance difference, doesn't it
make more sense to use an empty custom class instead of a generic
object?


Personally I wouldn't bother. At least not unless the code was in a
tight loop or something and profiling the code showed that it was a
bottleneck and needed some extra perf boost.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Keith Langer | last post by:
I would like to know why locking on a new object takes significantly longer than locking on a strongly typed object. All of the posts I've seen say that it is best to lock on a "new object", but...
4
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first...
10
by: Bob Day | last post by:
Using vs 2003, vb.net sql msde.. Consider the following code snippets. See **** for questions. All are shared and accessed by multiple threads simultaneiously. ' Instantiate per for this...
3
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however,...
7
by: SD | last post by:
I have a public object that I only want one thread to access at a time. However the access to this object is not limited to one procedure. Will SyncLock work in this case? If not what options do I...
1
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection...
3
by: Chris Dunaway | last post by:
I was using a Queue object like this to create my own specialized queue class for use with my own objects: Public Class MySpecializedQueue Private q As New Queue Public Sub Enqueue(obj As...
7
by: Brett | last post by:
Say I have a DLL with a method that does a file write. I call this DLL from an application and pass the file path and name to the FileWrite() method inside the DLL. The application is multi...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
5
by: Rory Becker | last post by:
I have had code in my Application_Start which is intended to run once at the start of my application's life. It loads connection information and similar from a known location. However I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.