ale*_*xey 5 .net c# synchronization semaphore
我有以下类来管理对资源的访问:
class Sync : IDisposable
{
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(20);
private Sync()
{
}
public static async Task<Sync> Acquire()
{
await Semaphore.WaitAsync();
return new Sync();
}
public void Dispose()
{
Semaphore.Release();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
using (await Sync.Acquire())
{
// use a resource here
}
Run Code Online (Sandbox Code Playgroud)
现在它允许不超过 20 个共享用法。
如何修改此类以允许每单位时间不超过 N 次共享使用(例如,每秒不超过 20 次)?