比如下面的代码,在 mres.Set() 没有被调用的时候,当前线程是不是会被一直阻塞(blocking)着?
var args = new SocketAsyncEventArgs();
using (var mres = new ManualResetEventSlim())
{
args.Completed += (s, e) => mres.Set();
if (socket.ConnectAsync(args))
{
if (!mres.Wait(timeout))
{
throw new TimeoutException("Could not connect to " + endpoint);
}
}
}
如果会阻塞当前线程, mres.Wait 返回 false 时是不是会释放当前线程?
如果会阻塞当前线程,在 async 方法中使用会带来什么后果?
socket.ConnectAsync
是 IO Pending, 这意味着 There is no thread, 与其花时间在这里等着,不如让当前的线程去干点别的在 Using Async with ManualResetEventSlim 也提到了
Using WatiHandle.WaitOne, or similar waits on a thread pool thread causes that thread to be busy waiting and thus consuming one ThreadPool Thread as seen in the debugger.