2017-05-18 11 views
0

내 프로젝트에 문제가 있습니다. 오류 코드 3을 던졌습니다.pthread_join 오류 코드 3

방금 ​​내가 한 일을 볼 수 있도록 제 코드를 추가합니다. main.cpp에 내가 스레드에 선언 한 다음 initRequestThreads (thread.h에) 스레드를 만들려면 보내십시오. main.cpp에서 주 프로세스가 기다릴 수있게합니다.

MAIN.CPP

pthread_t *requestersThreads = new pthread_t[Length_Tasks]; 
requestsPool->initRequestThreads(&requestersThreads); 
void* status; 


// wait for all requests threads 
for(t=0; t<Length_Tasks; t++) { 
    rc = pthread_join(requestersThreads[t], &status); 
    if (rc) { 
     cout<<"ERROR; return code from pthread_join() is "<< rc <<endl; 
     exit(-1); 
    } 
    cout<<"Main: completed join with REQUEST thread " << t <<" having a status of "<<(long)status<<endl; 
} 

// wait for all resolvers threads 
for(t=0; t<resolveThreadsAmount; t++) { 
    rc = pthread_join(reoslveThreads[t], &status); 
    if (rc) { 
     cout<<"ERROR; return code from pthread_join() is "<< rc <<endl; 
     exit(-1); 
    } 
    cout<<"Main: completed join with RESOLVER thread " << t <<" having a status of "<<(long)status<<endl; 
} 


delete[] tasks; 
delete[] TaskQueueRequests; 
delete[] TaskQueueResolves; 
//delete[] requestersThreads; 
//delete[] reoslveThreads; 

pthread_mutex_destroy(&TaskQueueResolves_lock); 
pthread_cond_destroy(&TaskQueueResolves_cond); 

ThreadPool.h

 void initRequestThreads(pthread_t **threads) 
    { 

     // add the attribute join for the threads 
     pthread_attr_t attr; 
     pthread_attr_init(&attr); 
     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 

     int rc; 

     cout << "DEBUG "<< __LINE__<<": numOfThreads:"<<numOfThreadsRequests<<endl; 
     for(long i = 0; i < numOfThreadsRequests; i++) 
     { 
      threads[i] = new pthread_t; 
      rc = pthread_create(&(*threads[i]), &attr, ThreadPool::OperationRequestThread, (void *)this); // create thread that get all the thread pool object(this) and preform OperationRequest function 
      if(rc) 
      { 
       cout <<"creating Request thread failed! Error code returned is:"+rc<<endl; 
       exit(-1); 
      } 
      cout << "DEBUG "<< __LINE__<<": Creating Request Thread #" << i+1 << "!\n"; 
     } 

     pthread_attr_destroy(&attr); 

}

+0

오류 번호 (''EINVAL'', ....)에 대해''pthread_join''의 반환 값을 테스트 했습니까? 원시 오류 값 이상으로 도움이됩니다. 귀하의 코드는 C와 C++의 엉망입니다. (''T *''대신에''vector''를 사용하십시오 ...) – nefas

답변

2

당신이 점점 오류 코드가 ESRCH이다 - 즉, 당신이하지 않는 가입하려고 스레드 있다.

그리고 그 이유는 스레드 ID를 처리하는 방법과 관련하여 코드에서 정의되지 않은 동작의 끔찍한 혼란입니다.

pthread_t *requestersThreads = new pthread_t[Length_Tasks]; 

이것은 N 스레드의 배열을 생성하고 당신이 지금

initRequestThreads(&requestersThreads); 

에 기능이 배열에 대한 포인터를 전달하는 것보다, 스레드 생성 루프에서, 당신은

threads[i] = new pthread_t; 
pthread_create(&(*threads[i]), &attr /*... */ 

여기에서는 배열을 완전히 엉망으로 만들고 정의되지 않은 동작을 트리거합니다. 귀하의 기능에서 threads은 배열이 아닙니다! 배열의 주소입니다. array subscript operator ([])으로 액세스 할 수 없습니다. 그리고 나머지는 단지 이미 여기에서 일어난 상해에 모욕을 추가하는 것입니다.

C++ 11 이상을 작성하는 경우 (2017 년과 마찬가지로) C++ 11 std::thread을 사용해야합니다. 어떤 이유로 든 C++ 2003에 바인딩되어 있다면 적어도 동적 배열에 대한이 끔찍한 비즈니스를 중지하고 그 포인터를 포인터에 전달해야하며 대신 함수에 출력 매개 변수로 std::vector<pthread_t>을 사용해야합니다.

+0

그것이 내가 남자를 한 것입니다. 나는 틀린 것을 보지 못합니다. 어쨌든, 그게 임무이기 때문에 나는 벡터를 사용할 수 없다. 여전히 작동하지 않는다. –