2012-03-14 2 views
1

pthread_detach 호출이 "잘못된 파일 설명자"오류로 실패합니다. 호출은 클래스에 대한 소멸자에 있으며 다음과 같습니다. -pthread_detach의 파일 설명자가 잘못되었습니다.

if(pthread_detach(get_sensors) != 0) 
    printf("\ndetach on get_sensors failed with error %m", errno); 

if(pthread_detach(get_real_velocity) != 0) 
    printf("\ndetach on get_real_velocity failed with error %m", errno); 

소켓을 사용할 때만이 오류를 처리했습니다. 내가 찾아야하는 pthread_detach 호출에서 이것이 일어날 수있는 원인은 무엇입니까? 또는 스레드 콜백에서 원인이 될 수있는 가능성이 있습니까? 이 경우 콜백은 다음과 같습니다.

void* Robot::get_real_velocity_thread(void* threadid) { 
    Robot* r = (Robot*)threadid; 
    r->get_real_velocity_thread_i(); 
} 

inline void Robot::get_real_velocity_thread_i() { 
    while(1) { 
     usleep(14500); 

     sensor_packet temp = get_sensor_value(REQUESTED_VELOCITY); 

     real_velocity = temp.values[0]; 
     if(temp.values[1] != -1) 
      real_velocity += temp.values[1]; 
    } //end while 
} 



/*Callback for get sensors thread*/ 
void* Robot::get_sensors_thread(void* threadid) { 
    Robot* r = (Robot*)threadid; 
    r->get_sensors_thread_i(); 
} //END GETSENSORS_THREAD 


inline void Robot::get_sensors_thread_i() { 
    while(1) { 

    usleep(14500); 

    if(sensorsstreaming) { 

     unsigned char receive; 
     int read = 0; 

     read = connection.PollComport(port, &receive, sizeof(unsigned char)); 

     if((int)receive == 19) { 

      read = connection.PollComport(port, &receive, sizeof(unsigned char)); 

      unsigned char rest[54]; 

      read = connection.PollComport(port, rest, 54); 

      /* ***SET SENSOR VALUES*** */ 
      //bump + wheel drop 
      sensor_values[0] = (int)rest[1]; 
      sensor_values[1] = -1; 
      //wall 
      sensor_values[2] = (int)rest[2]; 
      sensor_values[3] = -1; 
      ... 
      ... 
      lots more setting just like the two above 
      } //end if header == 19 
     } //end if sensors streaming 
    } //end while 
} //END GET_SENSORS_THREAD_I 

감사합니다.

답변

1

pthread_* 함수는 오류 코드를 반환합니다. 그들은 errno을 설정하지 않습니다. (글쎄, 그들은 물론, 어떤 방법 으로든 문서화 될 수 있습니다.)

코드는 pthread_detach에 의해 반환 된 값을 인쇄해야합니다.

단일 Unix Spec은이 함수에 대해 두 개의 반환 값, 즉 ESRCH (해당 ID별로 스레드 없음)과 EINVAL (스레드가 결합 가능하지 않음)을 문서화합니다.

개체 소멸자의 분리 스레드가 어리석은 것처럼 보입니다. 첫째, 그들이 결국 분리 될 것이라면, 왜 그렇게 만들지 않는가?

스레드가 파괴되는 개체를 사용할 위험이있는 경우 분리하지 말고 중지해야합니다. 나는. 당신은 어쨌든 그들이 닥쳐야 할 쓰레드에 지시하고, 더 이상 물체에 닿지 ​​않는 안전한 장소에 도착할 때까지 기다립니다. pthread_join이 유용합니다.

또한 소멸자로부터 그렇게하는 것이 조금 늦었습니다. 소멸자는 스레드를 실행하는 스레드가 해당 객체를 참조하는 유일한 스레드 일 때만 실행되어야합니다. 쓰레드가 여전히 객체를 사용하고 있다면, 당신은 그것들을 파괴하고 있습니다.

+0

나는 이것을 1 개 이상 upvote 할 수 있었으면 좋겠다. 여기에 몇 가지 좋은 점이있다. –

관련 문제