Show / Hide Table of Contents

lock Statement

The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.

The statement can be constructed using Lock method from any scope control object:

using System.Text;
using static DotNext.Metaprogramming.CodeGenerator;

Lambda<Action<StringBuilder>>(fun =>
{
    Lock(fun[0], () => 
    {
        Call(fun[0], nameof(StringBuilder.Append), 'a');
    });
})

//the generated code is

new Action<StringBuilder>(arg => 
{
    lock(arg)
    {
        arg.Append('a');
    }
});

Lock type is also supported for the lock statement.

  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾