GNU Radio 3.5.1 C++ API
thread_body_wrapper.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2009 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 #ifndef INCLUDED_THREAD_BODY_WRAPPER_H
00022 #define INCLUDED_THREAD_BODY_WRAPPER_H
00023 
00024 #include <gruel/api.h>
00025 #include <gruel/thread.h>
00026 #include <exception>
00027 #include <iostream>
00028 
00029 namespace gruel 
00030 {
00031 
00032   GRUEL_API void mask_signals();
00033 
00034   template <class F>
00035   class thread_body_wrapper
00036   {
00037     F           d_f;
00038     std::string d_name;
00039 
00040   public:
00041 
00042     explicit thread_body_wrapper(F f, const std::string &name="")
00043       : d_f(f), d_name(name) {}
00044 
00045     void operator()()
00046     {
00047       mask_signals();
00048 
00049       try {
00050         d_f();
00051       }
00052       catch(boost::thread_interrupted const &)
00053       {
00054       }
00055       catch(std::exception const &e)
00056       {
00057         std::cerr << "thread[" << d_name << "]: "
00058                   << e.what() << std::endl;
00059       }
00060       catch(...)
00061       {
00062         std::cerr << "thread[" << d_name << "]: "
00063                   << "caught unrecognized exception\n";
00064       }
00065     }
00066   };
00067 }
00068 
00069 #endif /* INCLUDED_THREAD_BODY_WRAPPER_H */