Solved

Is this safe with threads?

Posted on 2004-08-22
4
387 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:ErikPhilips
  • 2
4 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 50 total points
Comment Utility
depending on the operations you are doing on the named pipes you may actually need to look at mutexes to lock between processes (i.e. if you want bi-directional capabilities on the named pipe)

as for your other questions ... you should write a synchronized wrapper for the object if you intend to use it in multiple threads having a static variable actually males it more likely to have thread issues with multiple threads, the reason that most of the static methods throughout the framework are threadsafe is that they are atomic operations, they have no local data storage and operate upon data passed in to them only.
0
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
MRE /and the other common waithandles) are threadsafe by design. No need to worry, no lock needed.
0
 
LVL 4

Author Comment

by:ErikPhilips
Comment Utility
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 :)
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 350 total points
Comment Utility
Well, not guaranteed does not mean that they are not. In fact, I believe that the documentation has not been carefully edited for thread safety; you can read this "Any instance members are not guaranteed to be thread safe" comment at many places.

MRE, ARE, Mutexes, and Monitors are fully thread-safe. This must be so, since they are made to deal with thread concurrency. And if you look behind the scenes (that is, decompile the CLR), you should be able to see that this is true.

Also (concerning question 1), it does not matter which threads set the event. But following your description, it will in fact be your SNP.

Concerning Q3: Note that having a static instance of an object does not make its members static members! Therefore, it does NOT matter if you use static or instance members in regard to thread safety. The thing is that static members usually are state-less (e.g. they don't rely on other members), so that there cannot be a concurrency problem, why they are often thread-safe by design (without having been made thread-safe explicitly).
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feā€¦
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now