473,320 Members | 2,088 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,320 software developers and data experts.

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.WriteLine("Hello"); }
}

Zytan

Apr 25 '07 #1
9 6787
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.com

"Zytan" <zy**********@gmail.comwrote in message
news:11*********************@n35g2000prd.googlegro ups.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.WriteLine("Hello"); }
}

Zytan

Apr 25 '07 #2
"Zytan" <zy**********@gmail.comschrieb im Newsbeitrag
news:11*********************@n35g2000prd.googlegro ups.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**********@gmail.comwrote in message
news:11*********************@n35g2000prd.googlegro ups.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.WriteLine("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******@flounder.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******@flounder.comwrote in message
news:rn********************************@4ax.com...
| 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******@flounder.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**************@TK2MSFTNGP05.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_SECTION construct in the OS.
Both mutexes and CRITICAL_SECTIONs allow recursive acquisition, and indeed, the
CRITICAL_SECTION 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******@flounder.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
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...
11
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...
1
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...
2
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...
1
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)) ...
3
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...
1
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...
2
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.