2
private static Int64 DirectoryBytes(String path) 
    { 
     var files = Directory.EnumerateFiles(path); 
     Int64 masterTotal = 0; 
     ParallelLoopResult result = Parallel.ForEach<String, Int64>(
     files, 
     () => 
     { // localInit: Invoked once per task at start 
      // Initialize that this task has seen 0 bytes 
      return 0; // Set taskLocalTotal initial value to 0 
     }, 
     (file, loopState, index, taskLocalTotal) => 
     { // body: Invoked once per work item 
      // Get this file's size and add it to this task's running total 
      Int64 fileLength = 0; 
      FileStream fs = null; 
      try 
      { 
       fs = File.OpenRead(file); 
       fileLength = fs.Length; 
      } 
      catch (IOException) { /* Ignore any files we can't access */ } 
      finally { if (fs != null) fs.Dispose(); } 
      return taskLocalTotal + fileLength; 
     }, 
     taskLocalTotal => 
     { // localFinally: Invoked once per task at end 
      // Atomically add this task's total to the "master" total 
      Interlocked.Add(ref masterTotal, taskLocalTotal); 
     }); 
     return masterTotal; 
    } 

이것은 책에서 가져온 코드 조각입니다. 나는 이것에 의심의 여지가있다. 변수 tasklocaltotal은 스레드 수준 또는 작업 수준에 있습니다. 책에 따르면 태스크 레벨에 있지만 스레드가 프로그램을 실행하는 동안 변수가 그 값을 유지하는 방법보다 여러 태스크가 실행될 수 있기 때문입니다. 스레드 수준이어야한다고 생각합니다..NET 4.0의 Parallel.ForEach

이 개념에 대한 통찰력을 제공하고 더 많은 링크를 통해이 개념을 더 명확하게 읽을 수있는 곳을 읽을 수 있습니까?

답변

1

The overload of ForEach 여기서 사용되는 것은 마지막 매개 변수가 종료 된 각 작업에 대해 호출되는 작업임을 지정합니다.

각 작업의 끝에서 작업의 결과는 해당 작업의 로컬 합계를 사용하여 masterTotal을 증가시키는 Interlocked.Add 메서드로 전달됩니다.

taskLocalTotal => 
{ 
    // localFinally: Invoked once per task at end 
    // Atomically add this task's total to the "master" total 
    Interlocked.Add(ref masterTotal, taskLocalTotal); 
} 

그냥로 생각 (당신도 같이 쓸 수 있습니다) :

혼란스러운 부분은이 부분입니다

x => 
{ 
    // localFinally: Invoked once per task at end 
    // Atomically add this task's total to the "master" total 
    Interlocked.Add(ref masterTotal, x); 
} 

taskLocalTotal 불행하게도 작업의에서 여기에 같은 이름을 가진 동작.

그래서 같은 변수가 아니라 동일한 이름입니다.

+0

Erno를 지워 주셔서 감사합니다. – Vabs

+0

@Vabs - 문제 없습니다. 그건 그렇고, 내 대답 링크가 거의 동일한 코드를 포함하는 MSDN 페이지를 발견 했습니까? –

+0

예. 나는 그랬다. 예제를 생성하기 위해 MSDN 코드 샘플을 기본으로 사용할 수있는 책입니다. – Vabs