程序无缘无故挂起

本文关键字:挂起 无缘无故 程序 | 更新日期: 2023-09-27 18:06:15

我使用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]]);
       }
    }
  );

我不认为这是一个死锁,因为不可能有一个线程等待不同的资源,最终相互锁定。

它应该进入到这个方法中并递归地沿着后缀树向下走直到它等待直到没有线程向数组列表中添加任何对象

 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]]
              );
          }
       }

程序无缘无故挂起

抛出'flag'变量并使用锁语句。有可能有多个线程在你的临界区使用这段代码(即一个线程即将设置flag = true,而另一个线程刚刚将flag == false评估为true(顺便说一句,以后只使用!标志)

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]]);
    }
}