Reputation: 79003
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.
Upvotes: 3
Views: 3914
Reputation: 13257
POSIX doesn't support return codes from threads. I recommend you use a future: C++11 provides futures in the standard library, or if you're on an older version of C++, 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.
Upvotes: 5
Reputation: 40578
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:
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;
}
Upvotes: 1
Reputation: 910
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;
}
Upvotes: -1