
Untitled
By: a guest on
Apr 29th, 2012 | syntax:
None | size: 0.89 KB | hits: 14 | expires: Never
C# Multithreading application get its optimum using 16 thread although the computer has 32 cores
int N = 238;
int P = 16;
int Chunk = N / P;
AutoResetEvent signal = new AutoResetEvent(false);
// use a counter to reduce
int counter = P;
// kernel transitions
for (int c = 0; c < P; c++)
{ // for each chunk
ThreadPool.QueueUserWorkItem(delegate(Object o)
{
int lc = (int)o;
for (int i = lc * Chunk; i < (lc + 1 == P ? N : (lc + 1) * Chunk); i++)
{
// do something
}
if (Interlocked.Decrement(ref counter) == 0)
{
signal.Set();
}
}, c);
}
signal.WaitOne();
Parallel.For(0, N,
i =>
{
// do something
});