473,806 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursive locks work do not deadlock! why?

This program DOESN'T deadlock due to attempt to reenter the same
lock! Why?

private static object m_lock = new object();

static void Main(string[] args)
{
lock (m_lock) { MyFunc(); }
}

static void MyFunc()
{
lock (m_lock) { Console.WriteLi ne("Hello"); }
}

Zytan

Apr 25 '07 #1
9 6828
Zytan,

Why should it? The lock statement is meant to block out other threads
trying to run the same code, not the same thread.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Zytan" <zy**********@g mail.comwrote in message
news:11******** *************@n 35g2000prd.goog legroups.com...
This program DOESN'T deadlock due to attempt to reenter the same
lock! Why?

private static object m_lock = new object();

static void Main(string[] args)
{
lock (m_lock) { MyFunc(); }
}

static void MyFunc()
{
lock (m_lock) { Console.WriteLi ne("Hello"); }
}

Zytan

Apr 25 '07 #2
"Zytan" <zy**********@g mail.comschrieb im Newsbeitrag
news:11******** *************@n 35g2000prd.goog legroups.com...
This program DOESN'T deadlock due to attempt to reenter the same
lock! Why?
Hi Zytan,

you're code is all in one thread. Each thread can enter the same lock as
often as it want's to. This locks are only for inter thread synchronization ,
to prevent that more than one thread access some resource at the same time.

Christof
Apr 25 '07 #3
That behavior would be a Mutex, which acts like a binary semephore. A .Net
Monitor allows reentrance for the same thread - this behavior is most handy
(and required) in many situations.

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr
PSH Scripts Project www.codeplex.com/psobject
"Zytan" <zy**********@g mail.comwrote in message
news:11******** *************@n 35g2000prd.goog legroups.com...
| This program DOESN'T deadlock due to attempt to reenter the same
| lock! Why?
|
| private static object m_lock = new object();
|
| static void Main(string[] args)
| {
| lock (m_lock) { MyFunc(); }
| }
|
| static void MyFunc()
| {
| lock (m_lock) { Console.WriteLi ne("Hello"); }
| }
|
| Zytan
|
Apr 25 '07 #4
No. A mutex is NOT a binary semaphore; it is a MUTEX. A mutex allows recusive
acquisition by the owner thread. So the lock is following the definition of how a mutex
is supposed to work.
joe

On Wed, 25 Apr 2007 12:11:12 -0400, "William Stacey [C# MVP]" <wi************ @gmail.com>
wrote:
>That behavior would be a Mutex, which acts like a binary semephore. A .Net
Monitor allows reentrance for the same thread - this behavior is most handy
(and required) in many situations.
Joseph M. Newcomer [MVP]
email: ne******@flound er.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Apr 25 '07 #5
Right, thank Joseph. Have not used Mutex in a while... Primary difference
is a mutex can be used cross-process and a Monitor can not. Also a monitor
is typically faster as the fast path does not require kernel mode switch.
So "Monitor when you can, and Mutex when you have to".

--
William Stacey [C# MVP]
"Joseph M. Newcomer" <ne******@floun der.comwrote in message
news:rn******** *************** *********@4ax.c om...
| No. A mutex is NOT a binary semaphore; it is a MUTEX. A mutex allows
recusive
| acquisition by the owner thread. So the lock is following the definition
of how a mutex
| is supposed to work.
| joe
|
| On Wed, 25 Apr 2007 12:11:12 -0400, "William Stacey [C# MVP]"
<wi************ @gmail.com>
| wrote:
|
| >That behavior would be a Mutex, which acts like a binary semephore. A
..Net
| >Monitor allows reentrance for the same thread - this behavior is most
handy
| >(and required) in many situations.
| Joseph M. Newcomer [MVP]
| email: ne******@flound er.com
| Web: http://www.flounder.com
| MVP Tips: http://www.flounder.com/mvp_tips.htm
Apr 25 '07 #6
"William Stacey [C# MVP]" <wi************ @gmail.comwrote
Also a monitor is typically faster as the fast path does not require
kernel mode switch.
[I can't help myself. I can't. I have to pick nits. Ugh]

If I remember right, from reading the writings of Duffy & Richter, the
monitor does a SpinWait for a little while in the hope it can avoid a kernal
switch. After a bit it gives up, and hands itself off to the kernel.

It's still pretty darn fast though.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Apr 25 '07 #7
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:e9******** ******@TK2MSFTN GP05.phx.gbl...
"William Stacey [C# MVP]" <wi************ @gmail.comwrote
>Also a monitor is typically faster as the fast path does not require
kernel mode switch.

[I can't help myself. I can't. I have to pick nits. Ugh]

If I remember right, from reading the writings of Duffy & Richter, the
monitor does a SpinWait for a little while in the hope it can avoid a
kernal switch. After a bit it gives up, and hands itself off to the
kernel.
Yep, but only on multi-processor boxes (and multi cores) and (obviously)
when there is contention.

Willy.

Apr 25 '07 #8
The :"monitor" would be the C# representation of a CRITICAL_SECTIO N construct in the OS.
Both mutexes and CRITICAL_SECTIO Ns allow recursive acquisition, and indeed, the
CRITICAL_SECTIO N does a spin based on the premise that the locking interval is very short
most of the time. If the spin expires, then you get full kernel locking.
joe
On Wed, 25 Apr 2007 13:20:34 -0700, "Chris Mullins [MVP]" <cm******@yahoo .comwrote:
>"William Stacey [C# MVP]" <wi************ @gmail.comwrote
>Also a monitor is typically faster as the fast path does not require
kernel mode switch.

[I can't help myself. I can't. I have to pick nits. Ugh]

If I remember right, from reading the writings of Duffy & Richter, the
monitor does a SpinWait for a little while in the hope it can avoid a kernal
switch. After a bit it gives up, and hands itself off to the kernel.

It's still pretty darn fast though.
Joseph M. Newcomer [MVP]
email: ne******@flound er.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Apr 26 '07 #9
Everyone, thanks for the replies. After posting, I realized that
Enter does allow the same thread, and it DID make sense to allow this
to happen in my program, as it usually would for most programs.
Coming from using crticial sections in C++, this was a surprise. I
was bug hunting and when I seen my code enter the same lock twice, I
thought I found it until I realized it would have deadlocked on every
run, which it wasn't doing.

thanks for your quick responses!
Zytan

Apr 26 '07 #10

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

Similar topics

17
12840
by: Dr NoName | last post by:
Help! I have a table that multiple processes must be able to write to concurrently. However, it for some reason gets locked in exclusive mode. I narrowed it down to one SQL statement + some weirdness with foreign keys. To debug this, I opened two psql sessions and typed in the sql statements manually. Here is the situation: CREATE TABLE take2
11
14078
by: EoRaptor | last post by:
I`m exporting data from many Lotus Notes databases to a DB2 database using LotusScript. The LotusScript agents commit after EACH update/insert. Nevertheless, I keep getting transaction rollbacks on deadlock or timeout. When I get a snapshot for locks, I see that one of the export agents is getting a table lock rather than a row lock. I`m guessing this is what causes the time out. Questions: how do I determine whether it`s a deadlock or a...
1
1946
by: Mike | last post by:
Ok, why does the db2 diag log not reporting deadlocks and escalations any longer? Something I am doing wrong here? Also in the db2diag.log what is the difference between and TID and a PID. If I try and trace the application id of process that locked I do not find it. My guess is that since these programs make a connection and then disconnect that it is no longer there. In previous versions I thought I remember where the log actually showed...
2
7658
by: Jürgen Devlieghere | last post by:
Hi, We are creating event-driven multi-threaded applications on a daily basis. To help us solving deadlocks, we implemented a CriticalSection class that does dead-lock detection: an attempt to Enter() the critical section that would cause a deadlock logs the complete deadlock loop (thread / Critical section) and raises an exception. It has helped us a lot in the past. However, to prevent further deadlocks, and to get a higher...
1
1705
by: Willard Farwark | last post by:
Does this mean that only one of the two offending SQL statements finished? The other did not? 2006-10-04-22.16.02.828568 Instance:xrprod Node:000 PID:11482(db2agent (CLINK_P)) Appid:C0A80AD6.AE97.061005051554 data_management sqldEscalateLocks Probe:2 Database:LINK_P -- Lock Count, Target : 6491, 3245 -- Table (ID) Name : (1;14) LINK .VEHICLES_CA -- Locks, Request Type : 6486, X
3
7701
by: DaTurk | last post by:
I have a question concerning nested locks. I just noticed that I have an object declared in my parent class that I use as the lock. But what I noticed is that in one of the childs methods, I lock that object, then while still in the critical section I call one of my inherited methods, and lock the same object. So it's esentially this object _lock = new object();
1
3008
by: satish mullapudi | last post by:
Hi all, I am using DB2 v 8.2 , Win XP OS. I have creaed a sample situation which results in a deadlock created by appl1 & appl2.Now, I want to release one of the locks (using CLP, not Control Center) so as to release the deadlock.now how can I be sure that I am releasing the appropriate lock held by one of the aplication (say appl2). Using Control center -Application list -show lock chains we can say which applications hold which lock &...
2
1929
by: r034802n | last post by:
I'm using a universe database and am currently experiencing so many locks, just wondering if universe has a deadlock resolution mechanism.
7
6498
by: praveen | last post by:
Hi When does DB2 go for an IS (Intent Share) lock? IS mode is defined as a mode in which "The lock owner can read data in the locked table, but cannot update this data. Other applications can read or update the table." Why are concurrent applications allowed to update the table but the lock owner is not? In a normal SELECT query, it is observed that DB2 goes for NS (Next
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
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 we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.