ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
thread_parameter.h
Go to the documentation of this file.
1
7#ifndef _THREAD_CLASS_HEADER_
8#define _THREAD_CLASS_HEADER_
9
10#include <thread>
11#include <functional>
12
13namespace A_UTILS_NS
14{
15
17#ifndef __STDCALL
18#ifdef WIN32
19#define __STDCALL __stdcall
20#else
21#define __STDCALL
22#endif
23#endif
24
26#ifndef __THREAD_RESULT
27#ifdef WIN32
28#define __THREAD_RESULT unsigned long
29#else
30#define __THREAD_RESULT void*
31#endif
32#endif
33
34#ifdef _DEBUG
36#define _DEBUG_NAME_SIZE 512
37#endif
38
40#ifdef WIN32
41#define __THREADCALL __stdcall
42#else
43#define __THREADCALL
44#endif
45
50{
51 Default = 0,
52 Normal = 1,
54};
55
57#define A_UTILS_THREAD_PRIORITY_LOWEST 0.0
59#define A_UTILS_THREAD_PRIORITY_LOW 0.25
61#define A_UTILS_THREAD_PRIORITY_NORMAL 0.5
63#define A_UTILS_THREAD_PRIORITY_HIGH 0.625
65#define A_UTILS_THREAD_PRIORITY_HIGHEST 0.75
67#define A_UTILS_THREAD_PRIORITY_TIME_CRITICAL 1.0
68
70typedef std::vector<bool> tCpuAffinityMask;
71
138
143std::thread::native_handle_type get_native_handle_of_this_thread();
144
152tResult set_priority(std::thread::native_handle_type oThreadHandle,
153 tSchedulingClass eSchedulingClass, tFloat64 fPriority);
154
162tResult get_priority(std::thread::native_handle_type oThreadHandle,
163 tSchedulingClass& eSchedulingClass, tFloat64& fPriority);
164
171tResult set_cpu_affinity(std::thread::native_handle_type oThreadHandle,
172 const tCpuAffinityMask& oCpuAffinity);
173
180tResult get_cpu_affinity(std::thread::native_handle_type oThreadHandle,
181 tCpuAffinityMask& oCpuAffinity);
182
189tResult set_scheduling(std::thread::native_handle_type oThreadHandle,
190 const tSchedulingParameters& sScheduling);
191
198tResult get_scheduling(const std::thread::native_handle_type& oThreadHandle,
199 tSchedulingParameters& sScheduling);
200
207template <typename Callable, typename ...Args>
208void run_with_scheduling(tSchedulingParameters sScheduling, Callable&& pFunc, Args&&... args)
209{
211 pFunc(std::forward<Args>(args)...);
212}
213
221template <typename Callable, typename ...Args>
222void run_with_checked_scheduling(tSchedulingParameters sScheduling, Callable&& pFunc, Args&&... args)
223{
225 {
226 pFunc(std::forward<Args>(args)...);
227 }
228}
229
235 const tSchedulingParameters& sScheduling,
236 std::function<void()> pFunc);
237
246template <typename Callable, typename ...Args>
248 const tSchedulingParameters& sScheduling,
249 Callable&& pFunc, Args&&... args)
250{
251 // a lambda is not usable here, gcc 4.8 has a bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933
252 // so we use this bind as a workaround
253 auto pFuncCallable = std::bind(std::forward<Callable>(pFunc), std::forward<Args>(args)...);
254 return create_thread_with_scheduling_impl(oThread, sScheduling, pFuncCallable);
255}
256
264template <typename Callable, typename ...Args>
266 Callable&& pFunc, Args&&... args)
267{
268 std::thread oThread;
269 create_thread_with_scheduling(oThread, sScheduling,
270 std::forward<Callable>(pFunc),
271 std::forward<Args>(args)...);
272 return oThread;
273}
274
275} // namespace A_UTILS_NS
276
277#endif // _THREAD_CLASS_HEADER_
unsigned long tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
double tFloat64
type definition for Float64 (64bit double values) (platform and compiler independent type).
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
ADTF A_UTIL Namespace - Within adtf this is used as util or adtf_util.
Definition d_ptr.h:11
tResult get_cpu_affinity(std::thread::native_handle_type oThreadHandle, tCpuAffinityMask &oCpuAffinity)
Gets the CPU affinity of a thread.
std::thread::native_handle_type get_native_handle_of_this_thread()
Get the native handle for the current thread.
tSchedulingClass
Enum for different scheduling schemes.
@ Default
Choose whatever is applicable.
@ Normal
Basic non-realtime scheduling.
@ RealTime
Realtime scheduling.
tResult set_priority(std::thread::native_handle_type oThreadHandle, tSchedulingClass eSchedulingClass, tFloat64 fPriority)
Sets the priority of a thread.
tResult create_thread_with_scheduling(std::thread &oThread, const tSchedulingParameters &sScheduling, Callable &&pFunc, Args &&... args)
Creates a new thread that has the given scheduling parameters.
tResult get_scheduling(const std::thread::native_handle_type &oThreadHandle, tSchedulingParameters &sScheduling)
Gets the scheduling parameters of a thread.
tResult set_cpu_affinity(std::thread::native_handle_type oThreadHandle, const tCpuAffinityMask &oCpuAffinity)
Sets the CPU affinity of a thread.
std::vector< bool > tCpuAffinityMask
Container for a CPU Affinity mask.
tResult set_scheduling(std::thread::native_handle_type oThreadHandle, const tSchedulingParameters &sScheduling)
Sets the scheduling parameters of a thread.
tResult create_thread_with_scheduling_impl(std::thread &oThread, const tSchedulingParameters &sScheduling, std::function< void()> pFunc)
For internal use only.
void run_with_checked_scheduling(tSchedulingParameters sScheduling, Callable &&pFunc, Args &&... args)
Sets the given scheduling paremeters and executes the given method afterwards only if the scheduling ...
tResult get_priority(std::thread::native_handle_type oThreadHandle, tSchedulingClass &eSchedulingClass, tFloat64 &fPriority)
Gets the priority of a thread.
void run_with_scheduling(tSchedulingParameters sScheduling, Callable &&pFunc, Args &&... args)
Sets the given scheduling paremeters and executes the given method afterwards.
Compound class, that groups scheduling parameters.
tCpuAffinityMask oCpuAffinity
The CPU affinity. Empty -> run on all CPUs.
tSchedulingClass eSchedulingClass
The scheduling class.
bool operator==(const tSchedulingParameters &sOther) const
Equals operator.
tSchedulingParameters(const tSchedulingParameters &)=default
Copy constructor.
tSchedulingParameters(tSchedulingParameters &&)=default
Move constructor.
tSchedulingParameters(tSchedulingClass eSchedulingClass, tFloat64 fPriority)
Convenience constructor.
tFloat64 fPriority
The priority in the range [0.0, 1.0].
tSchedulingParameters(tSchedulingClass eSchedulingClass, tFloat64 fPriority, const tCpuAffinityMask &oCpuAffinity)
Convenience constructor.