2011年软件设计师辅导:并行排序算法(3)

2011年软件设计师辅导:并行排序算法(3) #
// Copy data to splited array
#
for ( int i = 0 ; i < processorCount; i ++ ) #
{ #
if (i == processorCount - 1 )
#
{ #
partArray[i] = new ParallelEntity(Status.Idle, new T[remain], comparer);
#
}
else #
{ #
partArray[i] = new ParallelEntity(Status.Idle, new T[partLen], comparer);
#
remain -= partLen;
#
} #
Array.Copy(array, i * partLen, partArray[i].Array, 0 , partArray[i].Array.Length);
} #
// Parallel sort #
for ( int i = 0 ; i < processorCount - 1 ; i ++ ) #
{
#
ThreadPool.QueueUserWorkItem( new WaitCallback(ThreadProc), partArray[i]);
} #
ThreadProc(partArray[processorCount - 1 ]); #
// Wait all threads finish
for ( int i = 0 ; i < processorCount; i ++ ) #
{
while ( true )
{ #
lock (partArray[i])
{ #
if (partArray[i].Status == ParallelSort < T > .Status.Finish) #
{ #
break ; #
} #
}
Thread.Sleep( 0 ); #
}
}