are multicast delegates thread-safe?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If you have a multicast delegate (say, an event in C#), is it safe to invoke this delegate at the same time from multiple threads (assuming, of course, that all of the methods that the delegate calls are themselves thread safe?) It seems this should be safe, since delegates are immutable, but the documentation says any instance members (of the MulticastDelegate class) are not guaranteed to be thread safe. Also assume I am invoking this delegate synchronously. Also, I am not concerned with issues related to the += or -= event operators, which create new delegates - assume I am not calling these operators at the time I am invoking the delegate

thanks
 
Hello,

Thanks for your post. Based on my experience and research, the invoking the
delegate is thread-safe.

Please feel free to let me know if you have any problems or concerns.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
thanks for your response and your research. It seems it should be thread safe, but the MSDN documentation gives me pause. So are you saying the documentation is incorrect? Can microsoft confirm this? I wonder why they say it is not "guaranteed" to be thread safe. Perhaps they can indicate which methods are or are not? Or maybe there is some usage scenario where some (planned?) derived class will not be thread safe? Perhaps it is not thread safe in the context of using these events as COM sourced events (connection points)

thank
-Andy
 
asanford said:
If you have a multicast delegate (say, an event in C#), is it safe to
invoke this delegate at the same time from multiple threads (assuming, of
course, that all of the methods that the delegate calls are themselves
thread safe?) It seems this should be safe, since delegates are immutable,
but the documentation says any instance members (of the MulticastDelegate
class) are not guaranteed to be thread safe. Also assume I am invoking this
delegate synchronously. Also, I am not concerned with issues related to the
+= or -= event operators, which create new delegates - assume I am not
calling these operators at the time I am invoking the delegate.

Hi asaford,

See if this link helps you:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05172001.asp

Look at the "Advanced Events" topic. I think you should be able to do the
same thing for delegates. Let me know if it solved your problem.

Hope this helps,
 
TT (Tom Tempelaere) said:
invoke this delegate at the same time from multiple threads (assuming, of
course, that all of the methods that the delegate calls are themselves
thread safe?)
Hi asaford,

See if this link helps you:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05172001.asp

Look at the "Advanced Events" topic. I think you should be able to do the
same thing for delegates. Let me know if it solved your problem.

Actually, it doesn't mention thread-safety of calling a delegate. Even the
documentation for delegate doesn't. If you are worried, perhaps you should
wrap and provide synchronization for the wrapper.
 
asanford said:
thanks. I know I can just put a lock in my method that invokes the
delegate, but I wanted to avoid that if Microsoft could tell me if the
multicast delegate invokation was in fact thread-safe (in contrast to what
the msdn doc says.) Does anyone have the answer?

I think that when it is possible in your program to add and remove delegates
from the event, at the same time you call (fire) the event, then it could be
unsafe. Since the documentation I read doesn't mention thread-safety, I
assume events are not thread-safe objects. So I would play safe and
synchronize.

Hope this helps,
 
Hi,

Please rest assured that multicast delegate invokation are thread-safe,
because the invocation code doesn't write to the delegate so there are no
race conditions. That is, delegates are immutable; they can't be changed
while you're using them.

Does this answer your question?

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hello, HuangTM

Yes, that does answer my question about delegate invokation, thanks. I guess that can only mean that the warning in the MSDN doc that says "...Any instance members are not guaranteed to be thread safe" may be a general statement the doc makes for virtually all classes, that doesn't accurately describe the methods assocated with invocation - and since the delegate object is immutable, I wouldn't think the statement in the doc would apply to ANY of the methods on instances (of delegates.) That point of confusion is why I submitted this question. Maybe the doc will be corrected at some point

thank you very much for your help
 
Hi,

Thanks for your feedback. The statement "...Any instance members are not
guaranteed to be thread safe" for MulticastDelegate is correct, however, it
can be misinterpretted because delegates are immutable. I will report your
feedback to the corresponding documentation team.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Tian,

Tian Min Huang said:
Please rest assured that multicast delegate invokation are thread-safe,
because the invocation code doesn't write to the delegate so there are no
race conditions. That is, delegates are immutable; they can't be changed
while you're using them.

What about events?
HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Cheers,
 
Events are delegates, thus they are thread-safe. class Event : MultiCast
delegate and adds some security featuers like the += -= operators.
 
Back
Top