473,789 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concurrency and delegates

I have a question about concurrency and delegates.

Say I have a class roughly like this:

public class Class1
{
public delegate void MyEventHandler( );
private event MyEventHandler onMyEvent;

public event OnMyEvent
{
add
{
onMyEvent += value;
}
remove
{
onMyEvent -= value;
}
}

private void MyEventHappened ()
{
if (onMyEvent != null)
onMyEvent();
}

If this class is used by multiple threads, it looks like there is a
race condition. If the last event is removed between the "if
(onMyEvent != null)" and the "onMyEvent( )", then "onMyEvent( )" will
throw a Null Reference Exception, right?

So the obvious fix is to use locks. The code for the event property
is obvious, and MyEventHappened now looks like this:

private void MyEventHappened ()
{
lock(this)
{
if (onMyEvent != null)
onMyEvent();
}
}

But now we're calling the delegate with the lock held. This seems bad
in general, since the event could start doing some long-running thing.
A more immediate problem happens if that event handler uses
form.Invoke, then the UI thread tries to call back into Class1 to do
something that requires a lock. Instant deadlock!

To fix the race condition, then, we have to either catch and ignore
the Null reference exception, or make a copy of it while it's locked,
then operate on our copy (which can't change behind our backs):

private void MyEventHappened ()
{
event MyEventHandler copyOfOnMyEvent ;
lock(this)
{
copyOfOnMyEvent = onMyEvent;
}
if (copyOfOnMyEven t != null)
copyOfOnMyEvent ();
}

(does that work? Does "copyOfOnMyEven t = onMyEvent" make a copy of
onMyEvent, or just store a reference to it?)

My question is, do people actually do this? It seems quite tedious,
and I've never seen it in sample code. Is there some reason I'm not
seeing why this isn't really a race condition? Is there some other
way to deal with this?

Thanks!

---Scott.
Nov 12 '07 #1
9 1897
Scott Gifford wrote:
My question is, do people actually do this? It seems quite tedious,
and I've never seen it in sample code. Is there some reason I'm not
seeing why this isn't really a race condition? Is there some other
way to deal with this?
The reason that you don't see this in any sample code is that normally
each thread would have their own instance of the class, so they would
not share the event.

--
Göran Andersson
_____
http://www.guffa.com
Nov 12 '07 #2
Scott Gifford <sg******@suspe ctclass.comwrot e:
I have a question about concurrency and delegates.
<snip>
If this class is used by multiple threads, it looks like there is a
race condition. If the last event is removed between the "if
(onMyEvent != null)" and the "onMyEvent( )", then "onMyEvent( )" will
throw a Null Reference Exception, right?
Yes.
So the obvious fix is to use locks. The code for the event property
is obvious, and MyEventHappened now looks like this:

private void MyEventHappened ()
{
lock(this)
{
if (onMyEvent != null)
onMyEvent();
}
}
No, it doesn't - or at least it shouldn't. Two reasons:

1) Locking on "this" is a bad idea. Lock on a private reference which
nothing else can lock on.

2) You shouldn't run other people's code (such as event handlers) while
holding the lock - as you mention below...
But now we're calling the delegate with the lock held. This seems bad
in general, since the event could start doing some long-running thing.
A more immediate problem happens if that event handler uses
form.Invoke, then the UI thread tries to call back into Class1 to do
something that requires a lock. Instant deadlock!
Exactly.
To fix the race condition, then, we have to either catch and ignore
the Null reference exception, or make a copy of it while it's locked,
then operate on our copy (which can't change behind our backs):
Just catching and ignoring the NRE wouldn't actually help - you still
need a lock (or at least a memory barrier) to make sure you get the
most recent value.
private void MyEventHappened ()
{
event MyEventHandler copyOfOnMyEvent ;
lock(this)
{
copyOfOnMyEvent = onMyEvent;
}
if (copyOfOnMyEven t != null)
copyOfOnMyEvent ();
}

(does that work? Does "copyOfOnMyEven t = onMyEvent" make a copy of
onMyEvent, or just store a reference to it?)
It takes a copy of the reference, which is good enough as delegates are
immutable.
My question is, do people actually do this? It seems quite tedious,
and I've never seen it in sample code. Is there some reason I'm not
seeing why this isn't really a race condition? Is there some other
way to deal with this?
Yes - most classes shouldn't try to make themselves thread-safe; they
should be used in a thread-safe way instead. Unless the class is doing
something inherently threaded (e.g. being a thread pool) I wouldn't try
to make the events thread-safe.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 12 '07 #3
On 2007-11-11 21:42:57 -0800, Scott Gifford <sg******@suspe ctclass.comsaid :
[...]
My question is, do people actually do this? It seems quite tedious,
and I've never seen it in sample code. Is there some reason I'm not
seeing why this isn't really a race condition? Is there some other
way to deal with this?
I believe that the basic answer is yes, if the event is supposed to be
thread-safe, people do actually do _something_ like this.

One technique I've seen suggested doesn't use locking at all. It just
copies the event. I'm not really clear on why that's considered
thread-safe (it doesn't seem to me that it would be), but it's
apparently common enough that the question of the assignment to a local
variable being optimized out has come up before. Unfortunately, I
don't recall any thread that ever provided any conclusive statements
one way or the other on either issue (whether the technique works, and
whether it can be accidently defeated by the optimizer). Here's one
example of such a discussion though:

http://groups.google.com/group/micro...b4b45776a7d7a0

Anyway,

that's a long way of saying that I think the last example you posted is
in fact the close to the recommended practice for thread-safe
event-raising. The only discrepancy would be the use of "this" as
opposed to a dedicated object instance for locking.

Jon Skeet wrote a good article on the topic. You might check it out,
assuming you haven't already:
http://www.pobox.com/~skeet/csharp/events.html

I'm not sure why he didn't mention that article in his own reply.
Maybe it's because it helps you understand how to make an event
thread-safe, even though his (perfectly reasonable) reply is to suggest
you just make sure you use the event in a thread-safe way instead. :)

Personally, I think either approach is fine. But you do need to
concern yourself with thread-safety one way or the other, for an event
used from multiple threads.

Pete

Nov 12 '07 #4
On Nov 12, 8:00 am, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:

<snip>
I believe that the basic answer is yes, if the event is supposed to be
thread-safe, people do actually do _something_ like this.

One technique I've seen suggested doesn't use locking at all. It just
copies the event. I'm not really clear on why that's considered
thread-safe (it doesn't seem to me that it would be),
It's thread safe in that it won't throw a NullReferenceEx ception.
However, it won't necessarily use the latest value of the variable
unless there are memory barriers in place.
but it's apparently common enough that the question of the
assignment to a local
variable being optimized out has come up before. Unfortunately, I
don't recall any thread that ever provided any conclusive statements
one way or the other on either issue (whether the technique works, and
whether it can be accidently defeated by the optimizer). Here's one
example of such a discussion though:
I believe the consensus is that under the MS CLR 2.0 memory model,
it's safe - but it's not *strictly speaking* safe under ECMA.
that's a long way of saying that I think the last example you posted is
in fact the close to the recommended practice for thread-safe
event-raising. The only discrepancy would be the use of "this" as
opposed to a dedicated object instance for locking.
And holding the lock while calling the other code - a definite "no-
no".
Jon Skeet wrote a good article on the topic. You might check it out,
assuming you haven't already:http://www.pobox.com/~skeet/csharp/events.html

I'm not sure why he didn't mention that article in his own reply.
I was late for work :)

<snip>

Jon

Nov 12 '07 #5
On 2007-11-12 00:56:31 -0800, "Jon Skeet [C# MVP]" <sk***@pobox.co msaid:
>One technique I've seen suggested doesn't use locking at all. It just
copies the event. I'm not really clear on why that's considered
thread-safe (it doesn't seem to me that it would be),

It's thread safe in that it won't throw a NullReferenceEx ception.
However, it won't necessarily use the latest value of the variable
unless there are memory barriers in place.
So, copying the event instance is an atomic operation? Is that because
there's some behind-the-scenes locking going on? Or is it because the
event can be copied in a single CPU-atomic operation?
I believe the consensus is that under the MS CLR 2.0 memory model,
it's safe - but it's not *strictly speaking* safe under ECMA.
So what are the chances it could change in the future? Is this
something that people should be cautioned to avoid relying on? Or is
this one of those things that so much code would break if Microsoft
changes the model that they would never change it in a way that would
break this?
>that's a long way of saying that I think the last example you posted is
in fact the close to the recommended practice for thread-safe
event-raising. The only discrepancy would be the use of "this" as
opposed to a dedicated object instance for locking.

And holding the lock while calling the other code - a definite "no-
no".
Well, the last example in Scott's post didn't have that problem. :)

Pete

Nov 12 '07 #6
Peter Duniho <Np*********@Nn OwSlPiAnMk.comw rote:
One technique I've seen suggested doesn't use locking at all. It just
copies the event. I'm not really clear on why that's considered
thread-safe (it doesn't seem to me that it would be),
It's thread safe in that it won't throw a NullReferenceEx ception.
However, it won't necessarily use the latest value of the variable
unless there are memory barriers in place.

So, copying the event instance is an atomic operation? Is that because
there's some behind-the-scenes locking going on? Or is it because the
event can be copied in a single CPU-atomic operation?
Reference assignments are always atomic - that's guaranteed under all
the memory models I've seen.
I believe the consensus is that under the MS CLR 2.0 memory model,
it's safe - but it's not *strictly speaking* safe under ECMA.

So what are the chances it could change in the future? Is this
something that people should be cautioned to avoid relying on? Or is
this one of those things that so much code would break if Microsoft
changes the model that they would never change it in a way that would
break this?
The latter - the situation suggested is basically insane. If you can't
rely on local variables being "safe" then life becomes impossible.
that's a long way of saying that I think the last example you posted is
in fact the close to the recommended practice for thread-safe
event-raising. The only discrepancy would be the use of "this" as
opposed to a dedicated object instance for locking.
And holding the lock while calling the other code - a definite "no-
no".

Well, the last example in Scott's post didn't have that problem. :)
Indeed - somehow I'd missed that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 12 '07 #7
On 2007-11-12 11:38:41 -0800, Jon Skeet [C# MVP] <sk***@pobox.co msaid:
>So, copying the event instance is an atomic operation? Is that because
there's some behind-the-scenes locking going on? Or is it because the
event can be copied in a single CPU-atomic operation?

Reference assignments are always atomic - that's guaranteed under all
the memory models I've seen.
Ah. The bit I was missing was that I forgot a delegate is immutable.
So assigning the reference from the event field assures a valid and
unchangeable reference to a delegate (null or otherwise). If somewhere
else, the event is subscribed or unsubscribed, the event field gets a
whole new reference, rather than the delegate it references being
changed. The original reference continues to contain what it always
did.

For some reason I was thinking that assigning the event does some sort
of cloning operation. I knew, based on other information, that it was
supposed to produce a copy of the event that was assured to not change.
But I forgot why that was (and completely overcomplicated the scenario
in the process :) ).

Pete

Nov 12 '07 #8
Peter Duniho <Np*********@Nn OwSlPiAnMk.comw rote:
So, copying the event instance is an atomic operation? Is that because
there's some behind-the-scenes locking going on? Or is it because the
event can be copied in a single CPU-atomic operation?
Reference assignments are always atomic - that's guaranteed under all
the memory models I've seen.

Ah. The bit I was missing was that I forgot a delegate is immutable.
So assigning the reference from the event field assures a valid and
unchangeable reference to a delegate (null or otherwise). If somewhere
else, the event is subscribed or unsubscribed, the event field gets a
whole new reference, rather than the delegate it references being
changed. The original reference continues to contain what it always
did.
Exactly.
For some reason I was thinking that assigning the event does some sort
of cloning operation. I knew, based on other information, that it was
supposed to produce a copy of the event that was assured to not change.
But I forgot why that was (and completely overcomplicated the scenario
in the process :) ).
Yeah, we (as a species) seem to have a habit of doing that. We make
life a lot harder and introduce magic when there's a much simpler
explanation.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 12 '07 #9
Hi Jon,

Thanks for your help! Your article that Peter linked to was very
helpful as well. A few questions below...

Jon Skeet [C# MVP] <sk***@pobox.co mwrites:
Scott Gifford <sg******@suspe ctclass.comwrot e:
[...]
you still need a lock (or at least a memory barrier) to make sure
you get the most recent value.
Without any kind of locking or memory barrier, the only problem is a
short window where another thread may have added or removed a
delegate, but our thread will still use an old copy, right?

[...]
>(does that work? Does "copyOfOnMyEven t = onMyEvent" make a copy of
onMyEvent, or just store a reference to it?)

It takes a copy of the reference, which is good enough as delegates are
immutable.
Ah, thanks, I didn't realize that delegates had that property.

It's too bad that multicast delegates weren't written to just do
nothing if they have no subscribers. That would make them less
error-prone and automatically threadsafe.

[...]
most classes shouldn't try to make themselves thread-safe; they
should be used in a thread-safe way instead. Unless the class is
doing something inherently threaded (e.g. being a thread pool) I
wouldn't try to make the events thread-safe.
Most of the classes I write that generate events run in their own
threads and call events for things that happen asynchronously (for
example, network data has arrived, user activity has occurred, new GPS
data is available, etc.). In these cases, it seems to me they have to
be thread-safe one way or another. Whenever an object decides it's no
longer interested in these events, it will need to unsubscribe. With
a race condition between the unsubscription and an asynchronous event
arriving, there's not really a safe time to unsubscribe.

There are certainly other options besides locking. For example, I
could require that the object device be closed before unsubscribing,
and promise not to generate events when the object is closed. But
this is just another way of implementing thread-safety, and doesn't
really seem any simpler than using locks.

Thanks for your help and for any further thoughts!

---Scott.
Nov 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2901
by: aurora | last post by:
Hello! Just gone though an article via Slashdot titled "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software" http://www.gotw.ca/publications/concurrency-ddj.htm]. It argues that the continous CPU performance gain we've seen is finally over. And that future gain would primary be in the area of software concurrency taking advantage hyperthreading and multicore architectures. Perhaps something the Python interpreter...
3
2449
by: Suzanne | last post by:
Hi All I'm having problems getting my data adapter to throw a concurrency exception with an INSERT command. I want to throw a concurrency exception if an attempt is made to enter a row into tb_table when a row with the same int_UID already exists in there. Here is my stored procedure: if not exists (select int_UID from tb_table where int_UID = @aint_UID)
2
5127
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
4
1561
by: Bob | last post by:
While testing my my program I came up with a consistency exception. My program consists of three datagridviews, One called dgvPostes which is the parent grid and its two children,one called dgvPlans and the other dgvTanks. What happens is as follows. I will either create or edit a record in the datagridview dgvPlans and call the Updatedb procedure (code below). The first save works OK. Then when that is done, on the same record I will try...
7
1772
by: William E Voorhees | last post by:
I'm updating an Access database in a windows multi-user environment. I'm using disconnected data I read data from an Access Data table to a data object I update the data object from a windows form I save the data from the data object to the Access Data table using a data adapter as follows:
3
1431
by: John | last post by:
Hi I have a vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. The problem I have is that I am getting a data concurrency error on mydataadapter.update() method. I know that there is no data concurrency problem as I am the only user testing the app. Obviously the error is misleading. What can I do from here to fix this problem? Thanks
19
1315
by: Chris Mullins [MVP - C#] | last post by:
I hit this bug last night in some test code I had written. It took a few minutes to figure out what the root cause was, and it left me thinking, "Wow. That was an interesting one that doesn't come up very often!". So, for fun, who sees the bug and can explain why it's happening? List<Threadthreads = new List<Thread>(); for (int i = 0; i < 2; i++) { Thread t = new Thread(delegate()
5
1853
by: John | last post by:
Hi I have developed the following logic to handle db concurrency violations. I just wonder if someone can tell me if it is correct or if I need a different approach.Would love to know how pros handle it. Thanks Regards
69
5594
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
0
10199
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.