Re: lock used in thread and by event



There's no such thing as "first thread's method" though.
Yes there are. Don't make this too hard...

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,
and there is an instance of that class. It has a method Y which uses
lock(x).
This class has also a eventhandler Z which is a method and has also
lock(x).
x is a synchronization variable.

So, in same principle to second thread. A class which implements
thread interface, has
an instance and we can call it a second thread. It has an C# Event to
be used to
fire a method in "first class". This method is called eventhandler.

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.

So the question is again, will any of these threads (first one and
second one) block to line lock(x)?

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).

If you know what is Java's ReentrantLock you should understand this
question :)

Cheers with Guiness!!



On 25 syys, 15:17, "Jon Skeet [C# MVP]" <sk...@xxxxxxxxx> wrote:
On Sep 25, 12:54 pm, uupi_...@xxxxxxxxx wrote:

No, this is the issue: first thread is using a method X that uses
lock(X).
Second thread is fireing an event that executes first threads method
that also uses lock(X).

There's no such thing as "first thread's method" though.

So is this situation same like e.g. one and only thread is calling
lock(X) two times and it does not block?

I don't know - I'd have to see the code.

To clearify again ;) is Event call from one thread to another threads
method that uses lock(X) {...} like Java's ReentrantLock?

An event call doesn't change threads (unless you explicitly call
Invoke or something like that). But to reiterate, there's no such
thing as "another thread's method". Methods don't belong to threads.

And yes, locks are re-entrant.

Jon

.


Loading