Boost logo

Boost Users :

Subject: [Boost-users] [Thread] join_all and interrupt_all playing together
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2009-10-21 01:16:05


I'm trying to set several threads running and have the main thread
signal an interrupt. I've included an example that shows what I'm
trying to accomplish. The threads that are created are associated with
thread_group and then a join_all is called. I would expect the main
thread to be able to call interrupt_all on the thread_group. Since this
wasn't working I looked at what the join_all and interrupt_all function
were actually doing. It turns out that thread_group has a mutex that is
being called for each function and since the mutex for join_all is in
use, the lock in interrupt_all is blocking. I understand why the mutex
is there but am unclear how interrupt_all is suppose to work with
join_all. To accomplish the functionality I need I have to use
create_thread and store the return pointer in a list of my own. Then
call interrupt on each pointer I stored. Which is basically duplicating
the storage of thread_group. This seem like a waste. I would
appreciate any advice.

Ryan

class Converter
{
public:
  Converter(void) {
    m_Threads.reset(new boost::thread_group);
  }

  void run(void) {
    m_Threads->add_thread(new
boost::thread(boost::bind(&Converter::processSureTrakMessage, this)));
    
//m_Threads_v.push_back(m_Threads->create_thread(boost::bind(&Converter::processSureTrakMessage,
this)));
    m_Threads->join_all();
    m_Threads.reset(new boost::thread_group);
  }

  void stop(void) {
    //for (std::vector<boost::thread *>::iterator iter =
m_Threads_v.begin(); iter != m_Threads_v.end(); ++iter) {
    // (*iter)->interrupt();
    //}
    m_Threads->interrupt_all();
  }

  void processMessage(void) {
    try {
      for (;;) {
        boost::this_thread::interruption_point();
      }
    }
    catch (...) {
      int bob = 1;
    }
    int bobo = 2;
  }

private:
  boost::scoped_ptr<boost::thread_group> m_Threads;
  boost::scoped_ptr<boost::thread>
m_IO_Service_Thread;
  std::vector<boost::thread *> m_Threads_v;

};

boost::function<void ()> console_ctrl_function;

BOOL WINAPI console_ctrl_handler(DWORD ctrl_type) {
  switch (ctrl_type) {
    case CTRL_C_EVENT:
    case CTRL_BREAK_EVENT:
    case CTRL_CLOSE_EVENT:
    case CTRL_SHUTDOWN_EVENT:
      console_ctrl_function();
      return TRUE;
    default:
      return FALSE;
  }
}

int main(int argc, char * argv[]) {
  Converter convert;
  console_ctrl_function = boost::bind(&Converter::stop, &convert);
  SetConsoleCtrlHandler(console_ctrl_handler, TRUE);

  convert.run();
 
  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net