Skip to content

Why locking on AsyncManualResetEvent WaitAsync? #1032

Answered by AArnott
patawa91 asked this question in Q&A
Discussion options

You must be logged in to vote

The lock doesn't protect the Task itself (as no one calls into it while within the lock anyway). Rather, the lock is to guard the state of our own AsyncManualResetEvent class itself. Consider where else the lock is taken in the class, around where that field is manipulated. Generally speaking, it could be that if another method is holding the lock, that the field's value might not be something we'd want to return from the WaitAsync method.
Another (very likely) reason is that we use a lock to avoid doing the more advanced memory barriers. Without a kind of memory barrier, we might return a stale value from the field long after it's been changed by a call to Set/Reset due to CPU core memor…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@patawa91
Comment options

@AArnott
Comment options

@patawa91
Comment options

Answer selected by patawa91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants