Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 信号量限制(.NET)是否阻止同一线程进入块?_C#_.net_Async Await_Semaphore_Reentrancy - Fatal编程技术网

C# 信号量限制(.NET)是否阻止同一线程进入块?

C# 信号量限制(.NET)是否阻止同一线程进入块?,c#,.net,async-await,semaphore,reentrancy,C#,.net,Async Await,Semaphore,Reentrancy,我已经阅读了信号量lim的文档 这表示如果将信号量LIM配置为: SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); 但是,它并不指示是否停止相同的线程访问该代码。这就产生了async和Wait。如果在方法中使用wait,控件将离开该方法,并在任何任务或线程完成时返回。在我的示例中,我使用一个带有异步按钮处理程序的按钮。它用“wait”调用另一个方法(Function1)。函数1依次调用 await Task.Run(() =>

我已经阅读了信号量lim的文档 这表示如果将信号量LIM配置为:

SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
但是,它并不指示是否停止相同的线程访问该代码。这就产生了async和Wait。如果在方法中使用wait,控件将离开该方法,并在任何任务或线程完成时返回。在我的示例中,我使用一个带有异步按钮处理程序的按钮。它用“wait”调用另一个方法(Function1)。函数1依次调用

await Task.Run(() => Function2(beginCounter));
在我的Task.Run()周围,我有一个信号量lim。它似乎确实阻止了同一个线程访问函数2。但这并不是从文档中保证的(正如我所读到的),我想知道这是否可以指望

我已经在下面发布了我的完整示例

谢谢

戴夫

如果您摆脱了信号量LIM调用,您将得到:

beginCounter: 1 threadId: 10
about to grab lock threadId: 10 beginCounter: 1
grabbed lock threadId: 10 beginCounter: 1
Function2 start threadId: 13 beginCounter: 1
beginCounter: 2 threadId: 10
about to grab lock threadId: 10 beginCounter: 2
grabbed lock threadId: 10 beginCounter: 2
Function2 start threadId: 14 beginCounter: 2
beginCounter: 3 threadId: 10
about to grab lock threadId: 10 beginCounter: 3
grabbed lock threadId: 10 beginCounter: 3
Function2 start threadId: 15 beginCounter: 3
Function2 end threadId: 13 beginCounter: 1
about to release lock threadId: 10 beginCounter: 1
released lock threadId: 10 beginCounter: 1
endCounter: 3 threadId: 10
Function2 end threadId: 14 beginCounter: 2
about to release lock threadId: 10 beginCounter: 2
released lock threadId: 10 beginCounter: 2
endCounter: 3 threadId: 10
发件人:

SemaphoreSlim类不会在对Wait、WaitAsync和Release方法的调用上强制执行线程或任务标识

换句话说,该类不会查看是哪个线程在调用它。这只是一个简单的计数器。同一个线程可以多次获取信号量,这与多个线程获取信号量的情况相同。如果剩余的线程数减少到0,则即使某个线程已经是获取该线程信号量的线程,如果它调用
Wait()
,它也会阻塞,直到其他线程释放该信号量

因此,关于
async
/
await
await
可以在启动它的同一线程中恢复,也可以不恢复,这一事实并不重要。只要您保持
Wait()
Release()
调用的平衡,它就会像人们希望和期望的那样工作

在您的示例中,您甚至异步等待信号量,因此不会阻塞任何线程。这很好,因为如果不这样,第二次按下按钮时就会死锁UI线程


相关阅读:


请特别注意关于重入/递归锁定的注意事项,尤其是使用
async
/
wait
。线程同步已经够棘手的了,而这个困难正是
async
/
await
旨在简化的。在大多数情况下,这一点意义重大。但当您将它与另一种同步/锁定机制混合使用时,情况就不一样了

    beginCounter: 1 threadId: 9
about to grab lock threadId: 9 beginCounter: 1
grabbed lock threadId: 9 beginCounter: 1
Function2 start threadId: 13 beginCounter: 1
beginCounter: 2 threadId: 9
about to grab lock threadId: 9 beginCounter: 2
beginCounter: 3 threadId: 9
about to grab lock threadId: 9 beginCounter: 3
Function2 end threadId: 13 beginCounter: 1
about to release lock threadId: 9 beginCounter: 1
released lock threadId: 9 beginCounter: 1
grabbed lock threadId: 9 beginCounter: 2
Function2 start threadId: 13 beginCounter: 2
endCounter: 3 threadId: 9
Function2 end threadId: 13 beginCounter: 2
about to release lock threadId: 9 beginCounter: 2
released lock threadId: 9 beginCounter: 2
endCounter: 3 threadId: 9
grabbed lock threadId: 9 beginCounter: 3
Function2 start threadId: 13 beginCounter: 3
Function2 end threadId: 13 beginCounter: 3
about to release lock threadId: 9 beginCounter: 3
released lock threadId: 9 beginCounter: 3
endCounter: 3 threadId: 9
beginCounter: 1 threadId: 10
about to grab lock threadId: 10 beginCounter: 1
grabbed lock threadId: 10 beginCounter: 1
Function2 start threadId: 13 beginCounter: 1
beginCounter: 2 threadId: 10
about to grab lock threadId: 10 beginCounter: 2
grabbed lock threadId: 10 beginCounter: 2
Function2 start threadId: 14 beginCounter: 2
beginCounter: 3 threadId: 10
about to grab lock threadId: 10 beginCounter: 3
grabbed lock threadId: 10 beginCounter: 3
Function2 start threadId: 15 beginCounter: 3
Function2 end threadId: 13 beginCounter: 1
about to release lock threadId: 10 beginCounter: 1
released lock threadId: 10 beginCounter: 1
endCounter: 3 threadId: 10
Function2 end threadId: 14 beginCounter: 2
about to release lock threadId: 10 beginCounter: 2
released lock threadId: 10 beginCounter: 2
endCounter: 3 threadId: 10