c# - Which features make a class to be thread-safe? -
c# - Which features make a class to be thread-safe? -
in msdn .net classes described this:
"this type thread safe."
or
"public static (shared in visual basic) members of type thread safe. instance members not guaranteed thread-safe.".
my question features create class thread-safe?
is there standard, recommendation or guidelines thread-safety programming?
when utilize lock(c#) keyword, means class thread-safe or not?
how evaluate thread-safety of class? there tests sure class 100% thread safe?
example:
public class myclass { public void method() { lock (this) { // now, class 100% thread-safe microsoft classes? } } type m_member1; type m_member2; } thanks
a class considered thread-safe if methods can invoked multiple threads concurrently without corrupting state of class or causing unexpected side-effects. there many reasons why class may not thread safe, although mutual reasons contains state corrupted on concurrent access.
there number of ways create class thread-safe:
make immutable, if class contains no state safe utilize concurrently multiple threads. employ locking cut down concurrency. however, no guarantee of thread safety, ensures block of code not executed concurrently multiple threads. if state stored between method invocations might still become inconsistent.how create thread-safe class depends on want class in question.
you need inquire yourself, need create class threadsafe? mutual model of ui frameworks there single ui thread. illustration in winforms, wpf , silverlight bulk of code executed ui thread means not have build thread-safety classes.
c# multithreading thread-safety
Comments
Post a Comment