数据并行(任务并行库) [英] Data Parallelism (Task Parallel Library)

查看:76
本文介绍了数据并行(任务并行库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有数十亿条记录,我必须对这些记录做一些长时间的计算。如果我使用顺序for循环,那么完成该任务需要大约7小时才能负担得起。为了克服这个问题,我正在使用 Parallel.For ,但是工作中发生了并发问题。



示例 -



1.运行Parallel.For对10条记录,它为每条记录创建10个线程。



2.执行,第一个线程完成所有计算并清除数据。



3.这里第二个或扩展线程在计算中得到错误说 - 数据不可用< br $> b $ b



我们如何解决这个并发问题?



我的代码的最终目标是为该记录创建一个新实例并在这些块中单独运行,不应影响其他实例。

Hi All,

I have billions of records and i have to do some long calculation on those records. If I am using a sequential for loop then it takes around 7hr to complete that task which is not affordable. To overcome with this, I’m using Parallel.For but the catch is concurrency is occurred in jobs.

Example -

1. Run Parallel.For on 10 records, it creates 10 threads for each record.

2. While executions, First Thread is done with all the calculation and clear the data.

3. Here Second or Reaming Threads getting error in the calculation says - data in not available


How can we resolve this concurrency probleam?

The ultimate goal of my code is to create a new instance for that record and run separately in these block and should not affect the others.

推荐答案

你需要为每个并发线程隔离线程本地数据。

首先阅读 TLS线程本地存储 [ ^ ],然后弄清楚如何在你的项目。

样本读取这个 [ ^ ],或谷歌搜索C#线程本地存储示例。



Parallel.For方法也存在于带有线程本地存储的味道。 参见 [ ^ ]。
You need to isolate thread local data for each concurrent threads.
First of all read about TLS Thread Local Storage[^], then figure out how to use them in your project.
For samples read this[^], or google for "C# thread local storage example".

The Parallel.For Method exist also in a flavour with thread local storage. See[^].


这篇关于数据并行(任务并行库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆