0

我正在使用 tpl 并行执行数据。问题是有时它会无缘无故挂起,给出这样的输出:

The thread '<No Name>' (0x4aa4) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bf4) has exited with code 0 (0x0).
The thread '<No Name>' (0x417c) has exited with code 0 (0x0).
The thread '<No Name>' (0x432c) has exited with code 0 (0x0).
The thread '<No Name>' (0x3ad0) has exited with code 0 (0x0).
The thread '<No Name>' (0x4440) has exited with code 0 (0x0).
The thread '<No Name>' (0x24e8) has exited with code 0 (0x0).
The thread '<No Name>' (0x3354) has exited with code 0 (0x0).
The thread '<No Name>' (0x4a30) has exited with code 0 (0x0).

该程序仍将执行,当我在 Visual Studio 中暂停它时,它会在我有 ForEach 的并行过程的地方暂停:

Parallel.ForEach
(
   hold1.remainingHolds.ToArray(), 
   subSequence =>
   {
      //only if subsequence has 1 common substring
      if (checkIfCommon(remainingSequence, subSequence.ToString()) == 1)
      {
         goDownLinkType2(subSequence.ToString().Split(','), 0, 
           (SuffixNode)hold1.childHolds[subSequence.ToString().Split(',')[0]]);
       }
    }
  );

我不认为它是死锁,因为没有任何情况下可以有一个线程等待不同的资源并最终相互锁定

它应该进入这个方法内部并递归下去一个后缀树,直到它会等到没有线程将任何对象添加到 arraylist

 Boolean flag = false;
 public void goDownLinkType2
      (string[] splittedString, 
       int index, SuffixNode currentNode)
       {
          Boolean writingFlag = false;
          if (index == 2)
          {
             while(writingFlag == false)
             {
                while(flag == true)
                {
        //blocked
                 }
                 if (flag == false)
                 {
                    flag = true;
                 if(!secondChoiceResults.Contains
                     (currentNode.representingStance.SequenceOfHolds))
                 {
                    Console.WriteLine("new addition");
                    secondChoiceResults.Add
                       (currentNode.representingStance.SequenceOfHolds,
                        currentNode.representingStance);
                  }
                  flag = false;
                  writingFlag = true;
               }
            }

         }
         else
         {
            int nextIndex = index + 1;
            goDownLinkType2
              (splittedString, 
               nextIndex,
               (SuffixNode)currentNode.childHolds[splittedString[nextIndex]]
              );
          }
       }
4

1 回答 1

1

折腾“标志”变量并使用锁定语句。使用此代码,您的关键部分可能有多个线程(即一个线程即将设置 flag = true 而另一个线程刚刚将 flag == false 评估为 true(顺便说一句,将来只需使用 !flag )

lock( obj )
{
    // critical section here
}

obj 只需要是对所有线程都可以访问的对象的引用。

这是我对您的代码的修改:

public void goDownLinkType2(string[] splittedString, int index, SuffixNode currentNode)
{
    Boolean writingFlag = false;
    if (index == 2)
    {
        while(writingFlag == false)
        {
            lock( this )
            //while(flag == true)
            //{
                //blocked
            //}
            //if (flag == false)
            {
                //flag = true;
                if (!secondChoiceResults.Contains(currentNode.representingStance.SequenceOfHolds))
                {
                    Console.WriteLine("new addition");
                    secondChoiceResults.Add(currentNode.representingStance.SequenceOfHolds, currentNode.representingStance);
                }
                //flag = false;
                writingFlag = true;
            }
        }


    }
    else
    {
        int nextIndex = index + 1;
        goDownLinkType2(splittedString, nextIndex, (SuffixNode)currentNode.childHolds[splittedString[nextIndex]]);
    }
}
于 2013-03-27T21:32:40.773 回答