2013-03-27 2 views
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). 

프로그램은 계속 실행된다 내가 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]]); 
     } 
    } 
); 

나는 그것의 서로 다른 자원을 기다리고 evetually 잠금 스레드가있을 수있는 경우가없는 죽은 잠금 원인을 생각 해달라고 서로

는이 방법 안에 입력해야하며 다우에게 전까지 접미사 트리를 이동 recursivley 기다릴거야. 어떤 스레드가 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]] 
      ); 
      } 
     } 
+0

는'Parallel.ForEach' 결국 종료합니까 그렇습니다. 이것은 디자인에 의한 것입니다. 스레드 풀의 모든 스레드를 종료합니다. 귀하의 요청을 만족시키기 위해 전자 회전합니다. 스레드가 종료되지 않으면 하나 이상의 스레드를 열어 놓고있는 경계 조건이 있습니까? –

+0

프로그램에서 종료 될 예정이지만이 경우 모든 데이터가 처리되지는 않았으므로이 단계에서 처리하면 안됩니다. 또한 나중에 도달하지 못한 중단 점에 도달하기로되어 있으며 프로그램은 foreach에서 중단됩니다. –

+0

출력을 처리 종료로 해석 할 가능성이 있습니다. 실제로 'Parallel.ForEach'가 더 이상 이러한 스레드를 필요로하지 않고 스레드 풀을 스레드 풀로 릴리스 할 수 있습니까? 좀 더 구체적인 대답을 원한다면 breakpoint에 대한 정보, 즉 코드의 위치, 코드, 충돌 이유 등을 알려주십시오. –

답변

1

에 모든 객체를 추가하지 않습니다 때까지 변수 '플래그'를 던져 및 잠금 문을 사용합니다. 이 코드로 하나 이상의 스레드가 중요 섹션에있을 수 있습니다 (즉, 다른 스레드가 flag == false를 true로 평가하는 동안 한 스레드가 flag = true를 설정하려고합니다 (나중에 btw! 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]]); 
    } 
} 
+0

고마워. –

+0

그런 일이 발생하면 즉, 두 개의 스레드가 동시에 입력되면 Visual Studio에서 예외로 지적하지 않겠습니까? –

+0

병렬 처리 문제를 디버깅하는 것은 매우 어렵고 대기중인 스레드는 디버거에 관한 한 문제가되지 않습니다. 아마도 오랫동안 비동기 작업이 완료 될 때까지 기다리는 중입니다. – Moho

관련 문제