Remove locking from Http11Connection using thread safe bool pattern
Problem
The locking in Http11Connection
is on our hot path. Locking incurs an overhead, and should be avoided when possible. The locking in Http11Connection
can be removed if we use the thread safe bool pattern explained in this stack overflow article.
https://stackoverflow.com/questions/29411961/c-sharp-and-thread-safety-of-a-bool
// default is false, set 1 for true.
private int _threadSafeBoolBackValue = 0;
public bool ThreadSafeBool
{
get { return (Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 1) == 1); }
set
{
if (value) Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 0);
else Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 0, 1);
}
}