lock keyword in C#

前端 未结 10 873
南旧
南旧 2020-11-30 03:54

I understand the main function of the lock key word from MSDN

lock Statement (C# Reference)

The lock keyword marks a statement block as

相关标签:
10条回答
  • 2020-11-30 04:08

    lock should be used around the code that modifies shared state, state that is modified by other threads concurrently, and those other treads must take the same lock.

    A lock is actually a memory access serializer, the threads (that take the lock) will wait on the lock to enter until the current thread exits the lock, so memory access is serialized.

    To answer you question lock is not needed in a single threaded application, and it does have performance side effects. because locks in C# are based on kernel sync objects and every lock you take creates a transition to kernel mode from user mode.

    If you're interested in multithreading performance a good place to start is MSDN threading guidelines

    0 讨论(0)
  • 2020-11-30 04:09

    Bear in mind that there might be reasons why your application is not as single-threaded as you think. Async I/O in .NET may well call-back on a pool thread, for example, as do some of the various timer classes (not the Windows Forms Timer, though).

    0 讨论(0)
  • 2020-11-30 04:14

    All the answers here seem right: locks' usefulness is to block threads from acessing locked code concurrently. However, there are many subtleties in this field, one of which is that locked blocks of code are automatically marked as critical regions by the Common Language Runtime.

    The effect of code being marked as critical is that, if the entire region cannot be entirely executed, the runtime may consider that your entire Application Domain is potentially jeopardized and, therefore, unload it from memory. To quote MSDN:

    For example, consider a task that attempts to allocate memory while holding a lock. If the memory allocation fails, aborting the current task is not sufficient to ensure stability of the AppDomain, because there can be other tasks in the domain waiting for the same lock. If the current task is terminated, other tasks could be deadlocked.

    Therefore, even though your application is single-threaded, this may be a hazard for you. Consider that one method in a locked block throws an exception that is eventually not handled within the block. Even if the exception is dealt as it bubbles up through the call stack, your critical region of code didn't finish normally. And who knows how the CLR will react?

    For more info, read this article on the perils of Thread.Abort().

    0 讨论(0)
  • 2020-11-30 04:15

    See the question about 'Mutex' in C#. And then look at these two questions regarding use of the 'lock(Object)' statement specifically.

    0 讨论(0)
  • 2020-11-30 04:16

    There is no point in having locks in the app if there is only one thread and yes, it is a performance hit although it does take a fair number of calls for that hit to stack up into something significant.

    0 讨论(0)
  • 2020-11-30 04:20

    You can have performance issues with locking variables, but normally, you'd construct your code to minimize the lengths of time that are spent inside a 'locked' block of code.

    As far as removing the locks. It'll depend on what exactly the code is doing. Even though it's single threaded, if your object is implemented as a Singleton, it's possible that you'll have multiple clients using an instance of it (in memory, on a server) at the same time..

    0 讨论(0)
提交回复
热议问题