如何在 TPL 中使用 Parallel For 代替 While
我想在语句中使用并行 for 而不是 while 语句。当我查看仅具有已知或变量计数的样本 Parallel For 运行时。
但我不知道我的循环将运行多少次,并且无法在运行时将其链接到变量。
我将尝试使用 TPL 和经典代码进行简单的性能测试。所以我正在编写一个模数类,它通过减量运算来计算模数。我的函数就像
long FindModulus(long n, int i)
{
while ( n >= i )
n -= i;
return n;
}
我的目标是用 Parallel For 循环替换这个循环
,我还想了解我可以将 Parallel For 与 if 和 break 语句一起使用。
我想我需要一个锁,因为 n 的值将在所有线程中更改,任何代码示例将不胜感激,
提前致谢
I want to use parallel for in a statement instead of while statement. When i look at samples Parallel For runs with only a known -or variable- count.
But i don't know how many times my loop will run, and its unable to link it to a variable in run time.
I'm gonna try a simple performance test with TPL and classic code. So i'm writing a modulus class which calculates modulus with decrement operation. My function is like
long FindModulus(long n, int i)
{
while ( n >= i )
n -= i;
return n;
}
My goal is replacing this loop with a Parallel For loop
and i also want to learn can i use Parallel For with an if and break statement.
I think i will need a lock because value of n will be changed in all threads, any code sample would be appreciated
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不知道循环将传递多少次,则
Parallel.For
不是一个选项。但您可以轻松地使用简单的任务并自己完成:if you don't know how many times the loop will pass
Parallel.For
is not an option. But you can easily use simple tasks and do it for yourself:我会像下面这样编写 MyParallel 类
并像这样使用它。
不要忘记锁定
condition
/action
中使用的共享对象(如果您修改它们)I would write
MyParallel
class like belowand use it like this.
Don't forget to lock the shared objects(if you modify them) used in
condition
/action
由于您没有在循环内做任何工作,因此任何示例都会被设计。但如果你坚持的话,这里有一个传达这个想法的例子(它会比同步版本慢,因为同步开销比工作本身更大):
Since you're not doing any work inside the loop any example would be contrived. But if you insist, here is an example that conveys the idea (it will be slower than the synchronous version because the synchronization overhead is larger than the work itself):
例如,Parallel.For 无法接收引用变量或 Func,因此我们仅限于使用良好的任务。下面是一个示例:
结果:
在槽位中执行任务:0
在槽中执行任务:1
在槽中执行任务:2
在槽中执行任务:3
等待任务槽
在槽中执行任务:2
等待任务槽
在槽中执行任务:1
等待任务槽
在槽中执行任务:3
等待任务槽
在槽中执行任务:1
等待任务槽
在槽中执行任务:0
等待任务槽
在槽中执行任务:3
等待任务槽
在槽中执行任务:2
...更多相同的。
完毕
Parallel.For can't receive a reference variable or a Func for example, so we are limited to using good ol' tasks. Here's an example:
Result:
Executing a task in slot: 0
Executing a task in slot: 1
Executing a task in slot: 2
Executing a task in slot: 3
Waiting for a task slot
Executing a task in slot: 2
Waiting for a task slot
Executing a task in slot: 1
Waiting for a task slot
Executing a task in slot: 3
Waiting for a task slot
Executing a task in slot: 1
Waiting for a task slot
Executing a task in slot: 0
Waiting for a task slot
Executing a task in slot: 3
Waiting for a task slot
Executing a task in slot: 2
... more of the same.
Done