Lock Keyword in C #

I understand the main function of the lock keyword from MSDN

Blocking statement (C # link)

The lock keyword denotes the lock statement as critical to receive a mutual exclusion lock for a given object that is executing and then release the lock.

When should you use a lock?

For example, this makes sense in multi-threaded applications because it protects data. But is it necessary when the application does not allocate other threads?

Are there any performance issues when using lock?

I just inherited an application that uses locks everywhere and it is single threaded and I want to know if I should leave them, are they even necessary?

Please note that this is rather a general question about knowledge, application speed is good, I want to know if this is a good design pattern that should be followed in the future, or should be avoided if absolutely necessary.

+42
multithreading c # design-patterns locking
Sep 12 '08 at 17:38
source share
10 answers

When should I use a lock?

Locking should be used to protect shared resources in multi-threaded code. For nothing else.

But is it necessary when the application does not allocate any other threads?

Absolutely not. This is just the time. However, do not forget that you are implicitly using system threads. For example, if you use asynchronous I / O, you can receive callbacks from a random thread, rather than from the original thread.

Are there any performance issues when using lock?

Yes. They are not very large in a single-threaded application, but why do not you need to make calls?

... if this is a good design template to follow in the future [?]

Locking all willingly or not is a scary design template. If your code is cluttered with random locking, and then you decide to use the background thread for some work, you are likely to run into deadlocks. Sharing a resource between multiple threads requires careful development, and the more you can isolate the complex part, the better.

+58
Sep 12 '08 at 17:49
source share

All the answers here seem correct: the usefulness of locks is to block threads from blocking code at the same time. However, there are many subtleties in this field, one of which is that blocked blocks of code are automatically marked as critical areas of the Common Language runtime.

The effect of marking the code as critical is that if the entire region cannot be fully executed, the runtime may assume that your entire application domain is potentially at risk and therefore unloads it from memory. To quote MSDN :

For example, consider a task that is trying to allocate memory while holding a lock. If memory allocation is not performed, interrupting the current task is not enough to ensure AppDomain stability, since there may be other tasks in the domain that expect the same lock. If the current task is completed, other tasks may be blocked.

Therefore, despite the fact that your application is single-threaded, it can be dangerous for you. Note that one method in a blocked block throws an exception that ultimately is not processed inside the block. Even if the exception is handled as it bubbles through the call stack, your critical area of ​​the code does not end normally. And who knows how the CLR will respond?

For more information, read this Thread.Abort () article on the dangers .

+7
Sep 13 '08 at 4:18
source share

Remember that there may be reasons why your application is not single-threaded, as you think. Asynchronous I / O in .NET can also cause a callback in the pool thread, for example, like some of the different timer classes (but not Windows Forms Timer).

+6
Sep 12 '08 at 17:52
source share

Generally speaking, if your application is single-threaded, you won’t get much benefit from the lock statement. Not knowing your application for sure, I don’t know if they are useful or not, but I suspect not. Also, if you use the blocking application all over the world, I don’t know that I will always feel confident that it works in a multi-threaded environment - did the original developer really know how to develop multi-threaded code, or they just add blocking statements everywhere in the vague hope that this would do the trick?

+2
Sep 12 '08 at 17:41
source share

a lock should be used around code that changes the general state, a state that is simultaneously changed by other threads, and these other steps should have the same lock.

The lock is actually a memory access serializer, threads (which accept the lock) will wait until the lock is entered until the current thread exits the lock, so memory access will be serialized.

To answer the question, question locking is not required in a single-threaded application and has performance side effects. because locks in C # are based on kernel synchronization objects, and every lock you take creates a transition to kernel mode from user mode.

If you're interested in multithreading performance, then a good place to start is MSDN Streaming Guidelines

+2
Sep 12 '08 at 17:48
source share

You may have performance problems with fixed variables, but you usually create your own code to minimize the amount of time it takes to lock the code.

Regarding the removal of locks. This will depend on what the code is doing. Despite the fact that it is single-threaded, if your object is implemented as Singleton, it is possible that you will have several clients using its instance (in memory, on the server) at the same time ..

+1
Sep 12 '08 at 17:43
source share

Yes, when using the lock, some performance will be observed, but it is generally careless in order not to matter.

The use of locks (or any other mutual exclusion or construction operator) is usually only required in multi-threaded scenarios where multiple threads (either your own decisions or from your caller) have the ability to interact with the object and change the underlying state or data. For example, if you have a collection that can be accessed by multiple threads, you do not want one thread to change the contents of this collection by deleting the item while another thread is trying to read it.

+1
Sep 12 '08 at 17:44
source share

A lock (token) is used only to mark one or more blocks of code that should not be launched simultaneously in several threads. If your application is single-threaded, it protects against a state that cannot exist.

And the lock invokes a performance call, adding instructions to check for simultaneous access before code execution. It should be used only where necessary.

+1
12 sept '08 at 17:46
source share

See the question about 'Mutex' in C #. And then consider these two questions regarding the use of the lock (Object) operator.

+1
Sep 12 '08 at 20:08
source share

It makes no sense to set locks in the application if there is only one thread, and yes, this is amazing in performance, although this hit requires quite a lot of calls in order to invest something significant.

0
Sep 12 '08 at 17:42
source share



All Articles