Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost thread and join

I've got this test case here that compiles with g++ theFile.cc -lboost_thread. When running the program it seems to be hanging at the join command. I'm not exactly sure why. It's like the interrupt_point() function isn't getting the join request from the main thread.

#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;
}

thanks for reading.

EDIT: also, if you remove the while loop the program exits cleanly... boost version 1.39.0

like image 832
Chris H Avatar asked Dec 10 '25 22:12

Chris H


1 Answers

It appears that your stop function doesn't in fact interrupt the thread. Calling join does not imply interruption, instead you have to do it explicitly by calling .interrupt() before joining if you wish to interrupt rather than wait for normal termination.

like image 106
Tronic Avatar answered Dec 13 '25 01:12

Tronic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!