Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Is this safe with threads?

Posted on 2004-08-22
4
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 200 total points
ID: 11865012
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
ID: 11865855
MRE /and the other common waithandles) are threadsafe by design. No need to worry, no lock needed.
0
 
LVL 4

Author Comment

by:ErikPhilips
ID: 11865861
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 1400 total points
ID: 11868103
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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