are multicast delegates thread-safe?

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
 
T

Tian Min Huang

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

Guest

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
 
T

TT \(Tom Tempelaere\)

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,
 
T

TT \(Tom Tempelaere\)

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

TT \(Tom Tempelaere\)

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,
 
T

Tian Min Huang

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

Guest

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
 
T

Tian Min Huang

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

TT \(Tom Tempelaere\)

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,
 
S

SpookyET

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top