threads.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_THREADS_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_THREADS_H_
27 
28 #include <thread>
29 
30 #if !defined(DISABLE_IO)
31 #include <cstring>
32 #include <iostream>
33 #endif
34 
35 #ifdef __unix__
36 #include <pthread.h> // required for set_thread_scheduling
37 #endif
38 
39 namespace corbo {
40 
41 namespace threads {
42 
48 inline bool set_thread_scheduling(std::thread& th, int priority) // priority 1-99
49 {
50 #ifdef __unix__
51  sched_param sch_params;
52  sch_params.sched_priority = priority;
53  if (pthread_setschedparam(th.native_handle(), SCHED_RR, &sch_params))
54  {
55 #if !defined(DISABLE_IO)
56  std::cerr << "Failed to set Thread scheduling : " << std::strerror(errno) << std::endl;
57 #endif
58  return false;
59  }
60  return true;
61 #else
62  return false;
63 #endif
64 }
65 
66 } // namespace threads
67 
68 } // namespace corbo
69 
70 #endif // SRC_CORE_INCLUDE_CORBO_CORE_THREADS_H_
bool set_thread_scheduling(std::thread &th, int priority)
Change the scheduling priority of a thread.
Definition: threads.h:48


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:52