Re: lock used in thread and by event



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

.



Relevant Pages

  • Re: device struct bloat
    ... Just locking the tree root is not enough? ... modifying operation to descend into the tree). ... I'd first start by asking if you want to lock all the children or the ... parent again. ...
    (Linux-Kernel)
  • Re: Lock MSSQL Table Row?
    ... I am using OleDb to connect to MSSQL. ... reads the parent table and 2 child tables into a DataSet. ... When a save happens, I want to lock the parent row, compare the "lock" ... Use the existing OleDbConnection with new OleDbCommands to do ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Race condition in debugger?
    ... What happens is that the debugger hangs at some ... that there's no lock protecting that the conditions in the ... >hold the parent lock across the bulk of kern_wait, ... the race has just occurred. ...
    (freebsd-current)
  • Re: device struct bloat
    ... acquires the semaphores for _all_ the devices in the tree. ... any time a driver's probe routine tries to register a child ... lock in order to probe drivers for the child. ... (it having a locked child, your parent, should be ...
    (Linux-Kernel)
  • Re: device struct bloat
    ... acquires the semaphores for _all_ the devices in the tree. ... lock in order to probe drivers for the child. ... parent again. ...
    (Linux-Kernel)