> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/framework/core/common/locker.md).

# Asynchronous Locks

Locker Synchronous and asynchronous mutex locks.

`Locker` is a lightweight mutex wrapper used to serialize critical sections in synchronous code and asynchronous code.

Both examples are taken from LockerTest; COUNT is the number of cycles of 500 defined in the fixture. The test verifies the mutual exclusion effect by concurrently incrementing the final count, rather than guessing the execution order with delays.

## Synchronous Critical Section

The synchronization code uses `Lock()` to obtain a releasable lock handle, which is automatically released when leaving the scope of `using`.

Source: [framework/Zongsoft.Core/test/Common/LockerTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/LockerTest.cs#L135) (excerpt; see source for context).

{% code title="LockerTest.cs" %}

```csharp
public void Lock()
{
	var count = 0;
	var locker = new Locker();

	Parallel.For(0, COUNT, i =>
	{
		using(locker.Lock())
		{
			count++;
		}
	});

	Assert.Equal(COUNT, count);
}
```

{% endcode %}

## Asynchronous Critical Section

Asynchronous code uses `LockAsync()` and releases the lock with `await using`.

Source: [framework/Zongsoft.Core/test/Common/LockerTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/LockerTest.cs#L189) (excerpt; see source for context).

{% code title="LockerTest.cs" %}

```csharp
public async Task LockAsync2()
{
	const int TIMES = 50;

	var count = 0;
	var locker = new Locker();

	await Parallel.ForAsync(0, TIMES, async (_, cancellation) =>
	{
		for(int i = 0; i < COUNT; i++)
		{
			await using(await locker.LockAsync(cancellation))
			{
				count++;
			}
		}
	});

	Assert.Equal(COUNT * TIMES, count);
}
```

{% endcode %}

LockAsync supports cancellation tokens, which is suitable for background tasks that need to cancel waiting for locks.

{% hint style="info" %}
If you need to create a change token, see [Notification](/documentation/en/framework/core/common/notification.md); if you need to express why the operation failed, see [OperationException](/documentation/en/framework/core/common/operation-exception.md).
{% endhint %}

## Related Resources

* [Locker.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/src/Common/Locker.cs)
* [LockerTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Common/LockerTest.cs)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/framework/core/common/locker.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
