How to make sure AsParallel() is working in Parallel
I got following code
foreach (var portfolio in portfolios.AsParallel()){ Some Processing ...}
and would like to make sure is working in parallel on different core. Is there any way to ensure this?
I tried
Parallel.ForEach(portfolios,
开发者_StackOverflow portfolio =>
{ Some Processing....}
and can see different threads are created and removed and also can see parallel task in Parallel Task window but not sure about Collection.AsParallel() also this is giving me some unpredictable behaviour.
Any better idea?
The point for AsParallel
is that you don't. You leave it to the CLR and it will decide how to run and optimise it, although most likely it will run in parallel.
You also need to explain what you mean by inconsistent. You are most likely bumping into threading and synchronisation issues. Any operation that is not thread-safe, is not a good candidate for AsParallel
unless you look after synchronisation of the state.
精彩评论