GNU Radio 3.7.0 C++ API
thread_body_wrapper.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2009,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 
00022 #ifndef INCLUDED_THREAD_BODY_WRAPPER_H
00023 #define INCLUDED_THREAD_BODY_WRAPPER_H
00024 
00025 #include <gnuradio/api.h>
00026 #include <gnuradio/thread/thread.h>
00027 #include <exception>
00028 #include <iostream>
00029 
00030 namespace gr {
00031   namespace thread {
00032 
00033   GR_RUNTIME_API void mask_signals();
00034 
00035     template <class F>
00036     class thread_body_wrapper
00037     {
00038     private:
00039       F d_f;
00040       std::string d_name;
00041 
00042     public:
00043       explicit thread_body_wrapper(F f, const std::string &name="")
00044         : d_f(f), d_name(name) {}
00045 
00046       void operator()()
00047       {
00048         mask_signals();
00049 
00050         try {
00051           d_f();
00052         }
00053         catch(boost::thread_interrupted const &)
00054           {
00055           }
00056         catch(std::exception const &e)
00057           {
00058             std::cerr << "thread[" << d_name << "]: "
00059                       << e.what() << std::endl;
00060           }
00061         catch(...)
00062           {
00063             std::cerr << "thread[" << d_name << "]: "
00064                       << "caught unrecognized exception\n";
00065           }
00066       }
00067     };
00068 
00069   } /* namespace thread */
00070 } /* namespace gr */
00071 
00072 #endif /* INCLUDED_THREAD_BODY_WRAPPER_H */