开发者

How to make sure AsParallel() is working in Parallel

开发者 https://www.devze.com 2023-03-05 10:29 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消