gpt4 book ai didi

c# - ParallelOptions、TaskCreationOptions 和 Task.Factory.StartNew 的正确使用?

转载 作者:行者123 更新时间:2023-12-05 00:29:03 30 4
gpt4 key购买 nike

请建议哪种方法是正确的(如果有的话)
有效使用 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?

最佳答案

您的代码和问题没有多大意义。 Task.Factory.StartNew()不接受 MaxDegreeOfParallelism ,因为它执行单个操作。 Parallel.Invoke()确实接受该参数,但是当您只有一个操作时使用该方法没有任何意义。

与其问这样一个非常具体的问题,我认为你应该退后一步,看看你真正想要达到的目标,然后可能提出一个新的问题。

编辑:现在我想我终于明白你想要做什么了:在每个核心上,你想要执行一个单独的循环。为此,您可以例如使用 Parallel.For() :

Parallel.For(0, Environment.ProcessorCount, parOpts, () =>
{
while (!tokenFor_task.IsCancellationRequested)
{
LongRunningMethod();
}
});

关于c# - ParallelOptions、TaskCreationOptions 和 Task.Factory.StartNew 的正确使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17879515/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com