If I have the following code:
object a = new object();
void DoSomething()
{
lock(a)
{
Thread.Sleep(5000);
}
}
Say I have 5 threads named a, b, c, d, and e.
If thread a calls this function first, followed in 10ms by threads b, c, d,
and e in that order. Which thread will acquired the lock next? b? WIll
the locks b, c, d, and e acquire the lock in that order EVERY TIME,
regardless of whether it is run on multiple processors, or single
processors, or hyperthreaded processors?
Can someone point me to some documentation that confirms or denies this?
Also, what about Mutex.WaitOne, Monitor.Enter, ManualResetEvent.WaitOne, and
AutoResetEvent.WaitOne?
Thanks,
-Chris 3 10196
Locking is first come first serve, but I wouldn't rely on the order because
any of the factors you mention below (multiple processors, scheduling, etc.)
might affect who the thread lets in and when.
Most if not all of the .NET wrappers behave this way, since they're built on
top of the Win32 locking primitives.
--
Klaus H. Probst, MVP http://www.vbbox.com/
"Chris" <ch*********************@technocisive.com> wrote in message
news:O$****************@TK2MSFTNGP10.phx.gbl... If I have the following code:
object a = new object();
void DoSomething() { lock(a) { Thread.Sleep(5000); } }
Say I have 5 threads named a, b, c, d, and e.
If thread a calls this function first, followed in 10ms by threads b, c,
d, and e in that order. Which thread will acquired the lock next? b? WIll the locks b, c, d, and e acquire the lock in that order EVERY TIME, regardless of whether it is run on multiple processors, or single processors, or hyperthreaded processors?
Can someone point me to some documentation that confirms or denies this?
Also, what about Mutex.WaitOne, Monitor.Enter, ManualResetEvent.WaitOne,
and AutoResetEvent.WaitOne?
Thanks,
-Chris
No, there is no guarantee about the order in which threads will obtain
ownership of the lock (the underlying critical section).
Willy.
"Chris" <ch*********************@technocisive.com> wrote in message
news:O$****************@TK2MSFTNGP10.phx.gbl... If I have the following code:
object a = new object();
void DoSomething() { lock(a) { Thread.Sleep(5000); } }
Say I have 5 threads named a, b, c, d, and e.
If thread a calls this function first, followed in 10ms by threads b, c, d, and e in that order. Which thread will acquired the lock next? b? WIll the locks b, c, d, and e acquire the lock in that order EVERY TIME, regardless of whether it is run on multiple processors, or single processors, or hyperthreaded processors?
Can someone point me to some documentation that confirms or denies this?
Also, what about Mutex.WaitOne, Monitor.Enter, ManualResetEvent.WaitOne, and AutoResetEvent.WaitOne?
Thanks,
-Chris
Thanks very much for the response.
-Chris
"Chris" <ch*********************@technocisive.com> wrote in message
news:O$****************@TK2MSFTNGP10.phx.gbl... If I have the following code:
object a = new object();
void DoSomething() { lock(a) { Thread.Sleep(5000); } }
Say I have 5 threads named a, b, c, d, and e.
If thread a calls this function first, followed in 10ms by threads b, c,
d, and e in that order. Which thread will acquired the lock next? b? WIll the locks b, c, d, and e acquire the lock in that order EVERY TIME, regardless of whether it is run on multiple processors, or single processors, or hyperthreaded processors?
Can someone point me to some documentation that confirms or denies this?
Also, what about Mutex.WaitOne, Monitor.Enter, ManualResetEvent.WaitOne,
and AutoResetEvent.WaitOne?
Thanks,
-Chris This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris |
last post by:
If I have the following code:
object a = new object();
void DoSomething()
{
lock(a)
{
Thread.Sleep(5000);
}
|
by: Sunny |
last post by:
Hi,
I can not understend completely the lock statement.
Actally what is locked:
1. the part of the code between {...}
or
2. the object in lock()
In the docs is written:
for 1: The lock...
|
by: ORC |
last post by:
Hi,
Will an exception or a return inside a lock statement block unlock the code
so that other threads will be able to enter the code?
Thanks
Ole
|
by: Jim S |
last post by:
My company's code often includes locks, using statements, and
try/catch blocks and ends up with a lot of brackets. Would it make
sense for C# to allow multiple items in these types of blocks?
...
|
by: ThunderMusic |
last post by:
Hi,
The subject says it all. Is there an equivalent, in VB.NET, for the C#
statement Lock(Object){}
Thanks
ThunderMusic
|
by: djc |
last post by:
I got great info on related question previously. This link
<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian
Gideon was especially informative.
1) the lock statement does...
|
by: Tao |
last post by:
hi.. Group,
is there any lock statement like C# in managed C++?
thanks
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
| |