Solved

Is this safe with threads?

Posted on 2004-08-22
4
388 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
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 350 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

21 Experts available now in Live!

Get 1:1 Help Now