Autoresetevent and ++i

  • Thread starter Thread starter Mike Feng
  • Start date Start date
M

Mike Feng

Guest
Question is simple: why the cnt is not 0?

AutoResetEvent m1 = new AutoResetEvent(false);
int i = 0;
int[] data = new int[0x100000];
Task t1 = Task.Run(() =>
{
Console.WriteLine("t1 run");
while (i < 0x0fffff)
{
m1.WaitOne();
++i;
}
Console.WriteLine("t1 done");

});

Task t2 = Task.Run(() =>
{
Console.WriteLine("t2 run");
while (i < 0x0fffff)
{
data = i;
m1.Set();
}
Console.WriteLine("t2 done");

});

Task.WaitAll(t1, t2);

int cnt = 0;
int min = 0x0fffff;
for (int j = 0; j < 0x0fffff; ++j)
{
if (data[j] != j)
{
min = Math.Min(min, j);
cnt++;
Console.WriteLine($"data[{j}]: {data[j]}");
}
}

Console.WriteLine($"min: {min}, count: {cnt}, {cnt * 1.0 / 0x0fffff}");





Mike Feng
Help me, help you

Continue reading...
 
Back
Top