thread_utils.h
Go to the documentation of this file.
00001 
00010 /*
00011  * Copyright 2014 Vladimir Ermakov.
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 3 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful, but
00019  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00021  * for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along
00024  * with this program; if not, write to the Free Software Foundation, Inc.,
00025  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00026  */
00027 
00028 #pragma once
00029 
00030 #include <thread>
00031 #include <cstdio>
00032 #include <sstream>
00033 #include <cstdarg>
00034 #include <pthread.h>
00035 
00036 namespace mavutils {
00037 
00047 inline bool set_thread_name(std::thread &thd, const char *name, ...)
00048 {
00049         pthread_t pth = thd.native_handle();
00050         va_list arg_list;
00051         va_start(arg_list, name);
00052 
00053         char new_name[256];
00054         vsnprintf(new_name, sizeof(new_name), name, arg_list);
00055         va_end(arg_list);
00056         return pthread_setname_np(pth, new_name) == 0;
00057 }
00058 
00062 template <typename Thread>
00063 inline bool set_thread_name(Thread &thd, std::string &name)
00064 {
00065         return set_thread_name(thd, name.c_str());
00066 };
00067 
00071 template <typename T>
00072 inline const char *to_string_cs(T &obj)
00073 {
00074         std::ostringstream ss;
00075         ss << obj;
00076         return ss.str().c_str();
00077 }
00078 
00079 }; // namespace mavutils


libmavconn
Author(s): Vladimir Ermakov
autogenerated on Wed Aug 26 2015 12:29:08