?
Solved

Is this safe with threads?

Posted on 2004-08-22
4
Medium Priority
?
395 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

743 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