Increase flow and join

I have this test case that compiles with g ++ theFile.cc -lboost_thread. When the program starts, it seems to hang in the connection command. I'm not quite sure why. It, as the interrupt_point () function, does not receive a connection 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 delete the while loop, the program crashes ... boost version 1.39.0

+3
source share
2 answers

It looks like your stop function does not actually interrupt the thread. Calling a connection does not mean aborting, instead you should do it explicitly by calling .interrupt () before joining if you want to abort and not wait for the normal termination.

+2

. , , , , - , TerminateThread pthread_cancel, .

, boost:: this_thread:: disable_interruption.

+1

Source: https://habr.com/ru/post/1726569/


All Articles