> ## Documentation Index
> Fetch the complete documentation index at: https://ancplua.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AL0058 - Avoid lock on this

> Locking on this exposes the lock target to external code, risking deadlocks

Source: [Al0057ToAl0060ThreadingAnalyzer.cs](https://github.com/ANcpLua/ANcpLua.Analyzers/blob/main/src/ANcpLua.Analyzers/Analyzers/Al0057ToAl0060ThreadingAnalyzer.cs)

## Description

Locking on `this` exposes the synchronization target to any code that holds a reference to the instance, creating deadlock risks.

## Bad Code

```csharp theme={null}
public void Update()
{
    lock (this) { _count++; }
}
```

## Good Code

```csharp theme={null}
private readonly Lock _syncRoot = new();
public void Update()
{
    lock (_syncRoot) { _count++; }
}
```

## Properties

* **Category**: Threading
* **Severity**: Warning
* **Enabled by default**: True
* **Code fix available**: False

## Configuration

```editorconfig theme={null}
dotnet_diagnostic.AL0058.severity = warning
```
