(もしあれば)適切である方法を提案正しいとParallelOptionsの 効率的に使用するため、TaskCreationOptionsと Task.Factory.StartNew(()=>。ParallelOptions、TaskCreationOptions、Task.Factory.StartNewを適切に使用していますか?
private void NeedToUse_MaxDegreeOfParallelism_Method1()
{
CancellationTokenSource tokenFor_task = new CancellationTokenSource();
ParallelOptions parOpts = new ParallelOptions();
//parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
//parOpts.TaskScheduler = TaskScheduler.Default;
TaskCreationOptions tco = new TaskCreationOptions();
tco = TaskCreationOptions.PreferFairness;
Task task = null;
task = Task.Factory.StartNew(() =>
{
while (!tokenFor_task.IsCancellationRequested)
{
LongRunningMethod();
}
}, tokenFor_task.Token, tco, TaskScheduler.Default);
}
private void NeedToUse_MaxDegreeOfParallelism_Method2()
{
//CancellationTokenSource tokenFor_task = new CancellationTokenSource();
ParallelOptions parOpts = new ParallelOptions();
parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
parOpts.TaskScheduler = TaskScheduler.Default;
TaskCreationOptions tco = new TaskCreationOptions();
tco = TaskCreationOptions.PreferFairness;
Task task = null;
task = Task.Factory.StartNew(() =>
{
while (!parOpts.CancellationToken.IsCancellationRequested)
{
LongRunningMethod();
}
}, parOpts.CancellationToken, tco, parOpts.TaskScheduler);
}
private void NeedToUse_MaxDegreeOfParallelism_Method3()
{
CancellationTokenSource tokenFor_task = new CancellationTokenSource();
ParallelOptions parOpts = new ParallelOptions();
//parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
//parOpts.TaskScheduler = TaskScheduler.Default;
TaskCreationOptions tco = new TaskCreationOptions();
tco = TaskCreationOptions.PreferFairness;
Task task = null;
task = Task.Factory.StartNew(() =>
{
Parallel.Invoke(parOpts,() =>
//while is already in LongRunningMethod() because can not be here
//while (!tokenFor_task.IsCancellationRequested)
//{
LongRunningMethod()
//}
);
}, tokenFor_task.Token, tco, TaskScheduler.Default);
}
private void NeedToUse_MaxDegreeOfParallelism_Method4()
{
CancellationTokenSource tokenFor_task = new CancellationTokenSource();
ParallelOptions parOpts = new ParallelOptions();
//parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
//parOpts.TaskScheduler = TaskScheduler.Default;
TaskCreationOptions tco = new TaskCreationOptions();
tco = TaskCreationOptions.PreferFairness;
Task task = null;
Parallel.Invoke(parOpts,() =>
task = Task.Factory.StartNew(() =>
{
while (!tokenFor_task.IsCancellationRequested)
{
LongRunningMethod();
}
}, tokenFor_task.Token, tco, TaskScheduler.Default)
);
}
現在、私はすべてのエラーを取得しないでください私が使用する必要のあるMaxDegreeOfParallelismを考慮していません。理想的には、Parallel.Invokeを使用しませんが、Task.Factory.StartNewにparOpts.MaxDegreeOfParallelismを含める方法は?
私は、可能な場合は、すべてのコアで並列に実行されるLongRunningMethodを()が必要です。 – as74