开发者

Parallel.For: Is it safe to lock the value?

Im trying to figure out the output of this code:

Dictionary<int, MyRequest> request = new Dictionary<int, MyRequest>();

for (int i = 0; 开发者_如何转开发i < 1000; i++ )
{
  request.Add(i, new MyRequest() { Name = i.ToString() });
}

var ids = request.Keys.ToList();

Parallel.For(0, ids.Count, (t) =>
{
  var id = ids[t];
  var b = request[id];

  lock (b)
  {
    if (b.Name == 4.ToString())
    {
      Thread.Sleep(10000);
    }

    Console.WriteLine(b.Name);
  }
});

Console.WriteLine("done");
Console.Read();

output:

789
800
875
.
.
.
4
5
6
7
done

MyRequest is just a dummy class used for demonstration (it is not doing anything but holding values). Is my lock blocking the execution or are the last 4 being put on their own thread?

This is a .NET 4.0 demo.

UPDATE Ok I did figure out they were on teh same thread, but i would still like to know if the lock does anything to block execution. I cant imagine it does.


If ids does not contain duplicates, that lock won't block anything. But if there are duplicates in ids, then yes, there might be contention at the lock, as different threads fight for access to the same request.


Your lock will only be blocking execution if the ids line up such that you retrieve the same request more than once. Since different names are being printed each time, that shouldn't be a concern.


Parallel.For uses a thread pool to process your loop. As soon as one of its threads is free, it assigns it to the next element. This is non-deterministic, because you don't know how many threads there are in the pool, and you don't control the CPU time given to each thread. This means that some threads may finish sooner or later than you would "naturally" expect.

Your lock isn't doing anything. A lock blocks delimits sections of code that attempt to use the same object. In your case, you're not ever using the same object twice in the loop. The fact that the last IDs processed seem consistent is probably purely coincidental.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜