multithreading - lock keyword in C# -


i understand main function of lock key word msdn

lock statement (c# reference)

the lock keyword marks statement block critical section obtaining mutual-exclusion lock given object, executing statement, , releasing lock.

when should lock used?

for instance makes sense multi-threaded applications because protects data. necessary when application not spin off other threads?

is there performance issues using lock?

i have inherited application using lock everywhere, , single threaded , want know should leave them in, necessary?

please note more of general knowledge question, application speed fine, want know if design pattern follow in future or should avoided unless absolutely needed.

when should lock used?

a lock should used protect shared resources in multithreaded code. not else.

but necessary when application not spin off other threads?

absolutely not. it's time waster. sure you're not implicitly using system threads. example if use asynchronous i/o may receive callbacks random thread, not original thread.

is there performance issues using lock?

yes. they're not big in single-threaded application, why make calls don't need?

...if design pattern follow in future[?]

locking willy-nilly terrible design pattern. if code cluttered random locking , decide use background thread work, you're run deadlocks. sharing resource between multiple threads requires careful design, , more can isolate tricky part, better.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -