Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

c# - EventWaitHandle.Set, wait timeout and number of released threads

From doc (EventWaitHandle.Set)

There is no guarantee that every call to the Set method will release a thread from an EventWaitHandle whose reset mode is EventResetMode.AutoReset. If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released. It is as if the second call did not happen. Also, if Set is called when there are no threads waiting and the EventWaitHandle is already signaled, the call has no effect.

Consider following code

private AutoResetEvent _are = new AutoResetEvent(false);

private void AnyMethod()
{
  await _are.Set();
}

Case 1

private async Task Method1(long timeout)
{  
  await _are.WaitOneAsync(timeout);
}

private async Task Method2()
{  
  await _are.WaitOneAsync();
}

private static Task WaitOneAsync(this WaitHandle waitHandle, long timeout)
{
    if (waitHandle == null)
        throw new ArgumentNullException("waitHandle");

    var tcs = new TaskCompletionSource<bool>();
    var rwh = ThreadPool.RegisterWaitForSingleObject(waitHandle,
        delegate { tcs.TrySetResult(true); }, null, timeout, true);
    var t = tcs.Task;
    t.ContinueWith( (antecedent) => rwh.Unregister(null));
    return t;
}

Case 2


private void Method1(int timeout)
{  
  _are.WaitOne(timeout);
}

private void Method2()
{  
  _are.WaitOne();
}

Imagine that both Method1 and Method2 are called and then AnyMethod releases one thread right in time when Method1 times out.

Is there guarantee about a thread releasement in the case 1 and 2 or Set method behavior applies generally to any thread releasement when either occurs close to another?

question from:https://stackoverflow.com/questions/65861581/eventwaithandle-set-wait-timeout-and-number-of-released-threads

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From doc...

The documentation quote describes what happens when Set is called and there are no threads waiting. The code in the rest of your question is all about calling Set when threads are waiting, so that paragraph of the docs doesn't apply.

Is there guarantee about thread releasement in case 1 and 2 or Set method behavior applies generally to any thread releasement when either occurs close to another?

Yes; one of the waiting threads will be released. If the event is Set before the timeout, it could be either thread; if the event is Set after the timeout, it will be the thread without the timeout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...