Changeset 281 in pacpusframework


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

Transporting and catching exceptions in threads.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Pacpus/kernel/ComponentBase.h

    r272 r281  
    3838class QWidget;
    3939
    40 namespace boost {
    41 namespace program_options {
     40namespace boost
     41{
     42class exception_ptr;
     43
     44namespace program_options
     45{
    4246    class options_description_easy_init;
    43 }
    44 }
     47} // namespace program_options
     48
     49} // namespace boost
    4550
    4651namespace pacpus {
     
    200205    /// called by the ComponentManager to start the component
    201206    int startComponent();
     207    void startComponentInThread();
     208    void startComponentWithException(boost::exception_ptr& error);
    202209
    203210    /// called by the ComponentManager to stop the component
  • 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.