Changeset 281 in pacpusframework for trunk/src/PacpusLib/ComponentBase.cpp


Ignore:
Timestamp:
Mar 20, 2014, 2:35:01 PM (11 years ago)
Author:
Marek Kurdej
Message:

Transporting and catching exceptions in threads.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PacpusLib/ComponentBase.cpp

    r280 r281  
    1111#include <Pacpus/kernel/PacpusException.h>
    1212
     13#include <boost/bind/bind.hpp>
     14#include <boost/exception/detail/exception_ptr.hpp>
     15#include <boost/exception/diagnostic_information.hpp>
     16#include <boost/exception/exception.hpp>
    1317#include <boost/program_options/parsers.hpp>
    1418#include <boost/program_options/variables_map.hpp>
     
    127131}
    128132
     133void ComponentBase::startComponentWithException(boost::exception_ptr& error)
     134{
     135    try {
     136        startActivity();
     137        error = boost::exception_ptr();
     138    } catch (...) {
     139        error = boost::current_exception();
     140    }
     141}
     142
     143void ComponentBase::startComponentInThread()
     144{
     145    boost::exception_ptr error;
     146    boost::thread t(
     147        boost::bind(
     148            &ComponentBase::startComponentWithException,
     149            this,
     150            boost::ref(error)
     151        )
     152    );
     153    t.join();
     154    if (error) {
     155        try {
     156            boost::rethrow_exception(error);
     157        } catch (boost::exception& e) {
     158            LOG_FATAL("[" << getName() << "]" << "\tboost::exception thrown: " << boost::diagnostic_information(e));
     159            //throw;
     160        }
     161    }
     162}
     163
    129164int ComponentBase::startComponent()
    130165{
     
    135170
    136171    setActive(true);
    137     boost::thread worker(&ComponentBase::startActivity, this);
     172    boost::thread worker(&ComponentBase::startComponentInThread, this);
     173    //boost::thread worker(&ComponentBase::startActivity, this);
    138174    //startActivity();
    139175    return true;
Note: See TracChangeset for help on using the changeset viewer.