Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net NET中的并行循环_.net_Parallel Processing - Fatal编程技术网

.net NET中的并行循环

.net NET中的并行循环,.net,parallel-processing,.net,Parallel Processing,iNewList每次都有2-4个长度 ProcessMatch平均需要2分钟 iOld是一个不平行的外环,不需要平行 基本循环: Int32 nearDupID = 1; for(int iOld = 0; iOld < 200, iOld++) { foreach (Int32 iNew in iNewList) { ProcssMatch(iOld, iNew, sIDwordIDs, ref nearDupID); // nearDu

iNewList每次都有2-4个长度
ProcessMatch平均需要2分钟
iOld是一个不平行的外环,不需要平行
基本循环:

Int32 nearDupID = 1;
for(int iOld = 0; iOld < 200, iOld++)
{
    foreach (Int32 iNew in iNewList)
    {
        ProcssMatch(iOld, iNew, sIDwordIDs, ref nearDupID); 
        // nearDupID is passed by ref and is revised in ProcssMatch 
        // the next call to ProcssMatch starts with the value from the last call
        // in parallel can I have shared nearDupID - update in one seen by others
    }
}
public void  ProcssMatch (int iOld, int iNew, sIDworkID[] sIDwordIDs, ref int nearDupID)
{  

     bool needNewID = false;
     loop on sIDwordIDs
     {         
        // do stufff 
        // this matches nothing - need a new nearDupID so I set needNewID to true
        if(needNewID)
        {
            nearDupID++;
            // i need a new nearDupID that is unique period
            // not just unique to this parallel process  
            needNewID = false;
        }
        // do more stuff
     }
}
但这并不是传递和共享nearDupID
问题是我需要在并行过程中共享nearDupID。
在ProcessMatch中,它将循环大约1000000次,nearDupID将增加大约1000次。
如何跨并行进程共享nearDupID


我发现它是连锁的,但我不知道如何传递。我想把nearDupID传给ProcssMatch。在ProcssMatch中,我需要将共享的nearDupID增加1并读取该值。就像我说的,procsmatch只会增加近dupid大约1000,运行2分钟

也许像这样的事情会有帮助?目前还不清楚nearDupId到底应该做什么

var nearDupId = 1;
Parallel.ForEach(iNewList, (iNew) =>
    {
        Interlocked.Increment(ref nearDupId);
        var copyOfNearDupId = nearDupId;
        ProcssMatch(iOld, iNew, sIDwordIDs, ref copyOfNearDupId);    
    });

这是一个令人困惑的问题。请包括我们复制当前处理所需的所有代码,即我们可以编译和运行的代码。然后我们就可以进行计时和并行化,并为您提供一个解决方案。@Enigmativity我只需要语法方面的帮助就可以跨并行共享nearDupID。有很多方法可以包含真正的代码。它正在处理从数据库读取的数百万条记录。从逻辑上讲,在进程之间共享nearDupID可能是不可能的。这就是为什么我们在提供帮助之前需要知道它的目的。这不仅仅是一个语法问题。@JohnFisher-nearDupID需要是唯一的。如果做不到,那么就做不到。我一直在研究,我认为Interlocted就是这么做的。@Frisbee-你应该写一些与实际代码结构相同的代码,但是没有DB调用等,因为我们无法运行它。但是,如果您可以使用
Thread.Sleep
模拟DB调用,那么我们就可以运行您的代码。我不认为这个问题很难回答,但目前缺乏答案直接关系到问题的质量。对不起,我还没有机会测试。我非常感谢您的帮助,但我不明白nearDupID的用途如何影响实现。正如我所说,nearDupID需要是唯一的。如果parallel one生成17(via++),parallel two生成17(via++),那么两个值为17的nearDupID实际上并不相同。如果您有两个电话接线员接受订单并生成订单号,如果他们不使用订单号主列表,则订单号将发生冲突。
var nearDupId = 1;
Parallel.ForEach(iNewList, (iNew) =>
    {
        Interlocked.Increment(ref nearDupId);
        var copyOfNearDupId = nearDupId;
        ProcssMatch(iOld, iNew, sIDwordIDs, ref copyOfNearDupId);    
    });