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
이 개념에 대한 통찰력을 제공하고 더 많은 링크를 통해이 개념을 더 명확하게 읽을 수있는 곳을 읽을 수 있습니까?
Erno를 지워 주셔서 감사합니다. – Vabs
@Vabs - 문제 없습니다. 그건 그렇고, 내 대답 링크가 거의 동일한 코드를 포함하는 MSDN 페이지를 발견 했습니까? –
예. 나는 그랬다. 예제를 생성하기 위해 MSDN 코드 샘플을 기본으로 사용할 수있는 책입니다. – Vabs