thread_utils.h
Go to the documentation of this file.
00001 
00010 /*
00011  * libmavconn
00012  * Copyright 2014,2015 Vladimir Ermakov, All rights reserved.
00013  *
00014  * This file is part of the mavros package and subject to the license terms
00015  * in the top-level LICENSE file of the mavros repository.
00016  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
00017  */
00018 
00019 #pragma once
00020 
00021 #include <thread>
00022 #include <cstdio>
00023 #include <sstream>
00024 #include <cstdarg>
00025 #include <pthread.h>
00026 
00027 namespace mavutils {
00028 
00038 inline bool set_thread_name(std::thread &thd, const char *name, ...)
00039 {
00040         pthread_t pth = thd.native_handle();
00041         va_list arg_list;
00042         va_start(arg_list, name);
00043 
00044         char new_name[256];
00045         vsnprintf(new_name, sizeof(new_name), name, arg_list);
00046         va_end(arg_list);
00047 #ifdef __APPLE__
00048         return pthread_setname_np(new_name) == 0;
00049 #else
00050         return pthread_setname_np(pth, new_name) == 0;
00051 #endif
00052 }
00053 
00057 template <typename Thread>
00058 inline bool set_thread_name(Thread &thd, std::string &name)
00059 {
00060         return set_thread_name(thd, name.c_str());
00061 };
00062 
00066 template <typename T>
00067 inline const std::string to_string_ss(T &obj)
00068 {
00069         std::ostringstream ss;
00070         ss << obj;
00071         return ss.str();
00072 }
00073 
00074 }; // namespace mavutils


libmavconn
Author(s): Vladimir Ermakov
autogenerated on Sat Jun 8 2019 19:55:13