The code sample "How to: Define Event Accessor Methods" at
http://msdn.microsoft.com/en-us/library/dw1dtw0d.aspx
appears to mutate the internal pE
without taking locks. (It doesn't look like Delegate::Combine
does anything magical that would prevent issues.) It also does
void raise() {
if (pE != nullptr)
pE->Invoke();
}
which can be problematic if pE
changes to null
between the check and the Invoke()
. I have two questions:
Am I right in that the existing code is not thread-safe?
Since I want a thread-safe version of the code, I was thinking of locking the
add
andremove
functions. Is it premature optimization to usevoid raise() { MyDel^ handler = pE; if (handler != nullptr) handler->Invoke(); }
or should I just lock that function too?