Re: lock used in thread and by event
- From: uupi_duu@xxxxxxxxx
- Date: Tue, 25 Sep 2007 06:33:13 -0700
Okey, thanks being "cool" :)
I made a simple sample app. There are two classes and they are both
running in threads.
First class has a method which use lock(this) and then it put itself
to sleep.
Another thread use a Event which launces handler in first class and
handler has also
a lock(this) synchronization. When first thread is in sleep second one
can't execute method
it fired using Event. Or this is what I discovered when debugging
code. So, Events and their handlers
are synchronized.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace TestLock
{
class Parent
{
public Parent()
{
}
public void JustACall()
{
lock (this)
{
Thread.Sleep(5000);
Console.WriteLine("JustACall");
}
}
public void CalledByEvent()
{
lock (this)
{
Console.WriteLine("CalledByEvent");
}
}
}
public delegate void DoHandler();
class Child
{
public event DoHandler _eventHandler;
private Parent _parent;
public Child(Parent parent)
{
_parent = parent;
_eventHandler += new DoHandler(_parent.CalledByEvent);
}
public void CallMe()
{
_eventHandler();
}
}
class Program
{
static void Main(string[] args)
{
Parent parent = new Parent();
Thread t = new Thread(new ThreadStart(parent.JustACall));
t.Start();
Child child = new Child(parent);
Thread t2 = new Thread(new ThreadStart(child.CallMe));
t2.Start();
Console.ReadKey();
}
}
}
Cheers!
On 25 syys, 16:07, "Jon Skeet [C# MVP]" <sk...@xxxxxxxxx> wrote:
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: Jon Skeet [C# MVP]
- 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
- Re: lock used in thread and by event
- From: Jon Skeet [C# MVP]
- lock used in thread and by event
- Prev by Date: How does the "this" keyword work in this context
- Next by Date: Best way to code the following
- 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
|