Link to home
Start Free TrialLog in
Avatar of ErikPhilips
ErikPhilipsFlag for United States of America

asked on

Is this safe with threads?

I'm looking rewriting a code example I found on the intarnet.  Here is a quick run down.

threadMain main() creates a new thread (public STATIC pipeManager), this in turn creates ServerNamedPipe threads.  Each ServerNamedPipe thread calls Form1.PipeManager.WakeUp().  WakeUp in turn calls Mre.Set().  (private ManualResetEvent Mre;)

Question 1: when WakeUp is called the method executes Mre.Set, is this run from the ServerNamedPipe thread or the pipeManger thread?  (I'm 95% sure its SNP thread)
Question 2: The help states the MREs "Any instance members are not guaranteed to be thread safe".  Does the Mre.Set() need to be be within a lock{} ?
Question 3: Or is all this OK as long as the pipeManager is a Static variable?


[Full code can be found here]
http://www.codeproject.com/csharp/DotNetNamedPipesPart1.asp
http://www.codeproject.com/csharp/DotNetNamedPipesPart2.asp
SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MRE /and the other common waithandles) are threadsafe by design. No need to worry, no lock needed.
Avatar of ErikPhilips

ASKER

Ok but what does the help mean when it says "Any instance members are not guaranteed to be thread safe" for MREs?

(and what about question 1 :)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial