lambda表达式(以及程度,匿名函数)是否闭包?
我对闭包的理解是它们是被视为对象的函数,这似乎是匿名函数和Lambda表达式所做的准确表示.
称他们为封闭是否正确?我知道由于lisp方言,闭包产生(或变得流行),但它是否也是一般编程术语?
感谢您提供的任何澄清!
我正在尝试实现Parallel.ForEach模式并跟踪进度,但我遗漏了一些有关锁定的内容.以下示例计数为1000时threadCount = 1,但不是threadCount> 1. 当正确的方法是什么时候?
class Program
{
static void Main()
{
var progress = new Progress();
var ids = Enumerable.Range(1, 10000);
var threadCount = 2;
Parallel.ForEach(ids, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, id => { progress.CurrentCount++; });
Console.WriteLine("Threads: {0}, Count: {1}", threadCount, progress.CurrentCount);
Console.ReadKey();
}
}
internal class Progress
{
private Object _lock = new Object();
private int _currentCount;
public int CurrentCount
{
get
{
lock (_lock)
{
return _currentCount;
}
} …Run Code Online (Sandbox Code Playgroud)