[英]AsParallel () and Any()?
I've seen this code which check a condition using AsParallel()
and Any()
: 我见过这个使用AsParallel()
和Any()
检查条件的代码:
bool IsAnyDeviceConnected()
{
return m_devices.Any(d => d.IsConnected);
}
and to make it faster : 并使其更快:
bool IsAnyDeviceConnected()
{
return m_devices.AsParallel().Any(d => d.IsConnected);
}
But looking at Any()
: 但是看着Any()
:
internal static bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
foreach (T element in source) {
if (predicate(element)) {
return true;
}
}
return false;
}
I don't see ( obviously ) - that it does care about cancellation of other workers - once found . 我没有看到( 显然 ) - 它确实关心取消其他工人 - 一旦找到 。
However - this ( other ) code does "finish - soon as possible" + cancel other future work : 但是 - 这个( 其他 )代码确实“尽快完成”+取消其他未来的工作:
bool IsAnyDeviceConnected()
{
var res = Parallel.ForEach(m_devices,
(d,loopState) => {
if (d.IsConnected)
loopState.Stop();
});
return !res.IsCompleted;
}
Question : 题 :
Does my diagnostics correct? 我的诊断是否正确? Does Any()
- once found item , doesn't cancel other threads ( in AsParallel context) Any()
- 一旦找到项目,不会取消其他线程(在AsParallel上下文中)
nb , my fear is that I might looking at the wrong source code. 嗯,我担心的是我可能会查看错误的源代码。
AsParallel()
returns a ParallelQuery
, so if you call AsParallel().Any(...)
you're not calling Enumerable.Any
, but ParallelEnumerable.Any
. AsParallel()
返回一个ParallelQuery
,所以如果你调用AsParallel().Any(...)
你不是在调用Enumerable.Any
,而是调用ParallelEnumerable.Any
。
The reference source code for ParallelEnumerable.Any
is here . ParallelEnumerable.Any
的参考源代码在这里 。
When you dig eg into the AnyAllSearchOperatorEnumerator
class, you see that a flag called resultFoundFlag
is used to tell other workers that a result is found so they can stop searching . 当您在AnyAllSearchOperatorEnumerator
类中挖掘时,您会看到一个名为resultFoundFlag
的标志用于告诉其他工作者找到结果,以便他们可以停止搜索 。
You're looking at the wrong code. 你看错了代码。 AsParallel
returns a ParallelQuery<TSource>
, and ParellelQuery
has another overload for Any
. AsParallel
返回一个ParallelQuery<TSource>
,而ParellelQuery
又有一个Any
重载。
'Any' creates a new AnyAllSearchOperator
object and aggregates it. 'Any'创建一个新的AnyAllSearchOperator
对象并聚合它。 If you dig deeper into that chain of method calls and objects you'll find that the QueryOpeningEnumerator
does support cancellation. 如果您深入研究方法调用和对象链,您会发现QueryOpeningEnumerator
支持取消。
Unfortunately the reference source links to those particular member functions are bugged. 不幸的是,那些特定成员函数的参考源链接被窃听。
You are looking at the wrong code. 您正在查看错误的代码。 ParallelEnumerable.AsParallel
returns a ParallelQuery<>
. ParallelEnumerable.AsParallel
返回一个ParallelQuery<>
。 ParallelEnumerable also defines its own Any extension method. ParallelEnumerable还定义了自己的Any扩展方法。
In order to specify cancellation, degrees of parallelism etc you need to use ParallelEnumerable's WithXXX extension methods like WithCancellation and WithDegreeOfParallelism . 要指定取消,并行度等,您需要使用ParallelEnumerable的WithXXX扩展方法,如WithCancellation和WithDegreeOfParallelism 。 ParallelEnumerable.Any doesn't allow you to specify those options to preserve a similar signature as Enumerable.Any
ParallelEnumerable.Any不允许您指定这些选项以保留与Enumerable.Any
类似的签名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.