Last night I discovered this piece of code (simplified version):
private void Foo()
{
bool b = false;
new Thread((ThreadStart)delegate { b = true;}).Start();
WaitForBool(b);
}
private void WaitForBool(bool b)
{
while (!b)
{
Thread.Sleep(1000);
}
}
I was immediately filled with disgust. How could someone write such a function (WaitForBool), which is one big bug? Of course the waiting will never be over, because bool is a value type, and no external influence can modify its value.
Later, I realized the fool is me.
I ran into this code a while back, and it did have a "ref" bool, which means it can be modified by the external thread. Resharper helpfully displayed a tip "this parameter can be declared as a value" , which I took without thinking too much about the consequences (I trust Resharper too much sometimes, it appears). So I deleted the "ref" with Resharper's help and created the bug myself without noticing.
(As a side note, of course this method of waiting for a bool should never be used - use ManualResetEvent instead).
Update
Fixed in the next Resharper build (Resharper 4.0.1, build 913), within a few hours of reporting it. Cool.