Re: lock used in thread and by event
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Tue, 25 Sep 2007 06:07:42 -0700
On Sep 25, 1:47 pm, uupi_...@xxxxxxxxx wrote:
There's no such thing as "first thread's method" though.
Yes there are. Don't make this too hard...
No, there really isn't. The sooner you express yourself clearly in
accepted terminology, the sooner we'll solve your problem.
We can also use Abstraction when posting to news.
If I say first thread's method that means there is a class that is a
thread,
But again, that breaks down. A class isn't a thread. Multiple threads
can run the same method in the same object at the same time.
In other words: it's not clear what you mean when you write "a class
that is a thread" or "a thread's method".
It *would* be clear if you'd produce a short but complete program,
however.
<snip>
So, some action now! "First method is executing method y and comes to
line where lock(x) is executed. At the same time "second thread" fires an
event which calls first thread's (synonym can be now class in this case ) eventhandler
(method z) which also executes it's lock(x) line.
If it's *genuinely* a second thread and the locks are *genuinely* to
the same reference,
then the second thread will block.
I know that if second thread (class) call directly without Event first
threads (class) method then
lock(x) will block in eventhandler z (we assume now that eventhandler
Z can be called directly like method).
Sorry, but you've lost me again. It would be *much* simpler to write
this in code rather than in text which uses terminology in a non-
standard way.
If you know what is Java's ReentrantLock you should understand this
question :)
I've already said that locks in C# (and .NET in general) are re-
entrant. Java's ReentrantLock has (as per the API documentation) "the
same basic behavior and semantics as the implicit monitor lock
accessed using synchronized methods" - and Java's synchronization is
very similar to .NET's.
In other words, within one thread you can indeed do:
lock (foo)
{
lock (foo) // No blocking here
{
...
}
}
But if you really, really have a second thread trying to acquire a
lock while another thread holds that lock, the second thread *will*
block until the holding thread releases the lock.
Jon
.
- Follow-Ups:
- Re: lock used in thread and by event
- From: uupi_duu
- Re: lock used in thread and by event
- References:
- lock used in thread and by event
- From: uupi_duu
- Re: lock used in thread and by event
- From: Jon Skeet [C# MVP]
- Re: lock used in thread and by event
- From: uupi_duu
- Re: lock used in thread and by event
- From: Jon Skeet [C# MVP]
- Re: lock used in thread and by event
- From: uupi_duu
- Re: lock used in thread and by event
- From: Jon Skeet [C# MVP]
- Re: lock used in thread and by event
- From: uupi_duu
- lock used in thread and by event
- Prev by Date: Re: Passing values between pages
- Next by Date: Re: Is [protected and/or internal] to be avoided?
- Previous by thread: Re: lock used in thread and by event
- Next by thread: Re: lock used in thread and by event
- Index(es):
Relevant Pages
|
Loading