should I lock event in the following case:
event foo;
thread A: will call foo += handler;
thread B: will call foo -= handler;
should I lock foo?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Locking on
foois a bad idea, because the value will change each time. You should lock on a variable which doesn’t change:Note that if you use a field-like event, like this:
then you’ll automatically get a “lock(this)” on add/remove, although you’d have to manually add it when fetching the handler before calling it (assuming you want to make sure you read the most recently written value). Personally I’m not a fan of locking on “this”, but you may not mind – and it certainly makes for simpler code.