473,471 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Synlock Q - Subprocedure with same object lock

Hi All,

If I have a procedure that obtains a sync lock:

Public Sub A
SyncLock Myobject
B()
End SyncLock

And another procedure which obtains the same synclock:

Public Sub B
SyncLock Myobject
... Do Stuff
End SyncLock

Will this cause a deadlock? Since A calls B - will the synclock be granted
to the subprocedure call?

Thanks.

May 30 '07 #1
7 1259
Hi,
It will have no effect as both calls happen on the same thread. Your thread
already holds the lock.
Robin
May 30 '07 #2
"Robin Tucker" <rt******@hotmail.comwrote in
news:f3*******************@news.demon.co.uk:
Hi,
It will have no effect as both calls happen on the same thread. Your
thread already holds the lock.
Ah OK - good to know.

Thanks.
May 30 '07 #3
On May 30, 8:52 am, Spam Catcher <spamhoney...@rogers.comwrote:
Hi All,

If I have a procedure that obtains a sync lock:

Public Sub A
SyncLock Myobject
B()
End SyncLock

And another procedure which obtains the same synclock:

Public Sub B
SyncLock Myobject
... Do Stuff
End SyncLock

Will this cause a deadlock? Since A calls B - will the synclock be granted
to the subprocedure call?

Thanks.
It will not cause a deadlock. The same thread is allowed to
recursively enter the same monitor. Just remember that the number of
Monitor.Enter and Monitor.Exit calls must match. That's pretty much a
given since you're using SyncLock.

May 30 '07 #4
It'll work, but it's a really bad idea. It works because Montiors - which
are the framework component under SyncLock - are reenterant.

Although it'll work, there are many pitfalls and deadlocks ahead of you,
along with a number of poorly concluded late-night debugging sessions...

If there's any way you can change your design to avoid this pattern, you
really should.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
Hi All,

If I have a procedure that obtains a sync lock:

Public Sub A
SyncLock Myobject
B()
End SyncLock

And another procedure which obtains the same synclock:

Public Sub B
SyncLock Myobject
... Do Stuff
End SyncLock

Will this cause a deadlock? Since A calls B - will the synclock be granted
to the subprocedure call?

Thanks.

May 30 '07 #5
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in
news:O2**************@TK2MSFTNGP05.phx.gbl:
It'll work, but it's a really bad idea. It works because Montiors -
which are the framework component under SyncLock - are reenterant.

Although it'll work, there are many pitfalls and deadlocks ahead of
you, along with a number of poorly concluded late-night debugging
sessions...

If there's any way you can change your design to avoid this pattern,
you really should.
Is there a better pattern? Basically A uses B's code. B is sometimes called
on it's own as well?
May 30 '07 #6
Without seeing what exactly you're doing, it's hard to say.

I can say that I've built a number of complex and highly concurrent
applications, and it's very, very, very rare that I end up re-entering a
lock. This is something did when I was first learning, and it bit me so
hard, so many times, that I know avoid it at (almost) all costs.

You want to lock as little as possible (but no less!), for as short a time
as possible. Sometimes this means doing:

Dim temp as new List(Of String);
synclock(mylock)
For Each s as string in _myImportantCollection
if s.StartWith("s") then temp.Add(s)
Next
end lock

' ... now perform operations on the temp collection.
' Notice that we're outside the lock...

There are all sorts of tricks. Encapsulation is really important - keep your
locks scoped at the right level, and directly relevant to the class at hand.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn*********************************@127.0.0.1 ...
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in
news:O2**************@TK2MSFTNGP05.phx.gbl:
>It'll work, but it's a really bad idea. It works because Montiors -
which are the framework component under SyncLock - are reenterant.

Although it'll work, there are many pitfalls and deadlocks ahead of
you, along with a number of poorly concluded late-night debugging
sessions...

If there's any way you can change your design to avoid this pattern,
you really should.

Is there a better pattern? Basically A uses B's code. B is sometimes
called
on it's own as well?

May 31 '07 #7
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in
news:#K**************@TK2MSFTNGP06.phx.gbl:
>
You want to lock as little as possible (but no less!), for as short a
time as possible. Sometimes this means doing:

Dim temp as new List(Of String);
synclock(mylock)
For Each s as string in _myImportantCollection
if s.StartWith("s") then temp.Add(s)
Next
end lock
That's for that one :-) Neat trick.
May 31 '07 #8

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

Similar topics

9
by: Kelly Vernon | last post by:
I have a standard ASP page that appends to an xml page. Currently if there is more than one person attempting to append to the same XML file at a time. One user will have the ability to append,...
2
by: steve | last post by:
I have Option Explicit ON by the way. I have declared a variable in one subprocedure which is being used by another subprocedure. But it doesn' seem to work. I am doing something similar to this...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
2
cindy2
by: cindy2 | last post by:
Hi everybody, In my vb2005-project I have a subprocedure (in a MODULE) like this: Public Sub MakeInputArray() Input(0) = Form2.TextBox1.Text Input(1) = Form2.TextBox2.Text End Sub...
2
by: WingSiu | last post by:
I am writing a Logging util for my ASP.NET application. I am facing mulit process problem. I developed a class LogFactory, and have a method called Get_Logger to create a FileLogger, which will...
11
by: fritzcwdev | last post by:
I have a class as follows: public class OperationFeedback { DateTime _startTime; public DateTime StartTime { get {
6
by: michael.spoden | last post by:
Hi, how can I fix lock-waits during an online backup? Is an online backup in DB2 V8.2 not realy online? I'm using DB2 V8.2 Fixpak 15 on Linux. The command to perform the backup is: db2 backup...
12
by: Zytan | last post by:
I have a Timer class set to trigger every second. The Tick function that is called every second uses a lock to prevent multiple ticks from executing the same code at the same time. The code...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.