views:

386

answers:

3

What is the standard way to get an exit code from a boost::thread ?
The docs don't seem to touch on this subject at all.

A: 

I don't know that the thread exit code is available as that's operating system specific. You could simulate passing an exit code or result code by doing something like this:

struct callable {
    int result;
    void operator()()
    {
        result = 42;
    }
};

void process_on_thread() {
    callable x;
    boost::thread processor(x);

    processor.join();
    int result = x.result;
}
David Smith
In order for that to work, you would have to pass x in a reference wrapper, since the constructor of boost::thread copies the argument.
Space_C0wb0y
+3  A: 

POSIX doesn't support return codes from threads. I recommend you use a future: the Boost.Thread library has had an excellent implementation of futures since version 1.41. This gives you a common syntax for both blocking and non-blocking communication, lets you return any type of object, and as the icing on the cake lets you propagate exceptions out of threads.

chrispy
+1 for mentioning futures
Sam Miller
A: 

Since Boost.Thread copies the functor you pass it David's solution won't work in any real-world example. You basically have two options:

  1. Allocate an array of exit codes and pass a reference to a unique exit code into the constructor of each functor. Pretty straight forward to implement.
  2. If you're only creating a single thread from a particular functor, then the functor can hold a shared smart pointer to a dynamically allocated exist code which can then be read by original functor object.

Here are examples of each method:

Method 1:

#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/shared_ptr.hpp>

#include <iostream>

struct thread_alarm
{
    thread_alarm(int secs, int &ec) : m_secs(secs), exit_code(ec) {  }
    void operator()()
    {
        boost::xtime xt;
        boost::xtime_get(&xt, boost::TIME_UTC);
        xt.sec += m_secs;

        boost::thread::sleep(xt);

        std::cout << "alarm sounded..." << std::endl;

        exit_code = 0xDEADBEEF;
    }

    int m_secs;
    int &exit_code;
};

typedef boost::shared_ptr<boost::thread> BoostThreadPtr;

int main(int argc, char* argv[])
{
    int secs = 1;
    int exit_codes[10];

    BoostThreadPtr threads[10];

    for (int i = 0; i<10; ++i) {
        std::cout << "setting alarm for 1 seconds..." << std::endl;
        thread_alarm alarm(secs, exit_codes[i]);
        threads[i] = BoostThreadPtr(new boost::thread(alarm));
    }

    for (int i = 0; i<10; ++i) {
        threads[i]->join();
        std::cout << "exit code == 0x" << std::hex << exit_codes[i] << std::endl;
    }
}

Method 2:

#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/shared_ptr.hpp>

#include <iostream>

struct thread_alarm
{
    thread_alarm(int secs) : m_secs(secs) { exit_code = IntPtr( new int(0) ); }
    void operator()()
    {
        boost::xtime xt;
        boost::xtime_get(&xt, boost::TIME_UTC);
        xt.sec += m_secs;

        boost::thread::sleep(xt);

        std::cout << "alarm sounded..." << std::endl;

        *exit_code = 0xDEADBEEF;
    }

    int m_secs;

    typedef boost::shared_ptr<int> IntPtr;
    IntPtr exit_code;
};

int main(int argc, char* argv[])
{
    int secs = 5;
    std::cout << "setting alarm for 5 seconds..." << std::endl;
    thread_alarm alarm(secs);
    boost::thread thrd(alarm);
    thrd.join();
    std::cout << "exit code == 0x" << std::hex << *(alarm.exit_code) << std::endl;
}
Robert S. Barnes