2009-12-25 4 views
3

g ++ theFile.cc -lboost_thread로 컴파일하는 테스트 케이스가 있습니다. 프로그램을 실행할 때 join 명령에 매달려있는 것처럼 보입니다. 왜 그랬는지 정확히 모르겠습니다. 그것은 interrupt_point() 함수가 주 스레드로부터 조인 요청을받지 못하는 것과 같습니다.스레드를 높이고 조인

#include <iostream> 
#include <stdio.h> 
#include <signal.h> 
#include <fstream> 
#include <boost/thread.hpp> 

using namespace std; 


////////////////////////////////// 

    class SerialnIMU { 
    public: 
    ~SerialnIMU(); 

    void start(); 
    void stop(); 
    private: 
    boost::thread readThread; 

    class SerialReader { 
    public: 

     void operator()(); 
    private: 
    }; 
    }; 

//////////////////////////////// 


SerialnIMU::~SerialnIMU() { 
    stop(); 
} 

void SerialnIMU::start() { 
    readThread = boost::thread(SerialReader()); 
    cout<<"Created: "<<readThread.get_id()<<endl; 
} 

void SerialnIMU::stop() { 
    cout<<"Join: "<<readThread.get_id()<<endl; 
    cout<<"Joining serial thread..."<<endl; 
    readThread.join(); 
    cout<<"Done."<<endl; 
} 

//////////////////////serial thread////////////////// 

void SerialnIMU::SerialReader::operator()() { 
    cout<<"Serial reading thread started..."<<endl; 
    try { 
    while(true) { 
     boost::this_thread::interruption_point(); 
    } 
    } catch(...) { 
    cout<<"exception was thrown."<<endl; 
    } 
    cout<<"Serial reading thread stopped."<<endl; 
} 


int main(int argc, char **argv) { 

    SerialnIMU imu; 

    imu.start(); 
    imu.stop(); 
    return 0; 
} 

감사합니다.

편집 : 당신은 while 루프를 제거하면 또한, 프로그램은 정상적으로 종료 ... 부스트 버전 1.39.0

답변

2

정지 기능이 실제로 스레드를 방해하지 않는 것 같습니다. join 호출은 인터럽트를 암시하지 않고, 정상적인 종료를 기다리지 않고 인터럽트를 원한다면 합류하기 전에 .interrupt()를 호출하여 명시 적으로 처리해야합니다.

1

스레드가 중단 예외를 잡을 무기한 처리를 유지할 수 있습니다. 중단이 최종화 되려면 예외가 잡히지 않아야합니다. 그렇지 않으면 처리를 포착하고 중지 할 수 있습니다. 또는 TerminateThread 나 pthread_cancel과 같이 좀 더 파괴적인 것을 사용할 수 있습니다.이 경우 항상 모든 정리 작업을 수행 할 수는 없습니다.

스레드가 boost :: this_thread :: disable_interruption으로 중단을 비활성화 할 수 있습니다.

+0

우리가 중단 예외를 포착하지 않는다면 멈출 것입니까? –

관련 문제