YARP
Yet Another Robot Platform
ThreadImpl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_IMPL_THREADIMPL_H
8 #define YARP_OS_IMPL_THREADIMPL_H
9 
10 #include <yarp/os/Semaphore.h>
11 
12 #include <atomic>
13 #include <thread>
14 
15 namespace yarp {
16 namespace os {
17 namespace impl {
18 
23 {
24 public:
25  virtual ~ThreadImpl();
26 
27  int join(double seconds = -1);
28  virtual void run();
29  virtual void close();
30 
31  // similar to close, but it does not call join (does not wait for thread termination)
32  void askToClose();
33 
34  // should throw if no success
35  virtual bool start();
36 
37  bool isClosing();
38  bool isRunning();
39 
40  virtual void beforeStart();
41  virtual void afterStart(bool success);
42 
43  virtual bool threadInit();
44  virtual void threadRelease();
45 
46  static int getCount();
47 
48  // get a unique key
49  long int getKey();
50  static long int getKeyOfCaller();
51 
52  //should become private, when the thread is friend
53  void notify(bool s);
54  void notifyOpened(bool s)
55  {
56  opened = s;
57  }
58  void synchroWait();
59  void synchroPost();
60 
61  int setPriority(int priority = -1, int policy = -1);
62  int getPriority();
63  int getPolicy();
64  long getTid();
65 
66  long tid{-1};
68 
69  static void yield();
70 
71 private:
72  int defaultPriority{-1};
73  int defaultPolicy{-1};
74  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::thread) thread;
75  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) active{false};
76  bool opened{false};
77  bool closing{false};
78  bool needJoin{false};
79 
80  yarp::os::Semaphore synchro{0};
81 
82  bool initWasSuccessful{false};
83 };
84 
85 } // namespace impl
86 } // namespace os
87 } // namespace yarp
88 
89 #endif // YARP_OS_IMPL_THREADIMPL_H
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:26
An abstraction for a thread of execution.
Definition: ThreadImpl.h:23
void notifyOpened(bool s)
Definition: ThreadImpl.h:54
std::thread::id id
Definition: ThreadImpl.h:67
ContainerT::value_type join(typename ContainerT::const_iterator begin, typename ContainerT::const_iterator end, const typename ContainerT::value_type &separator=", ")
Utility to join the elements in a container to a single string separated by a separator.
Definition: string.h:93
void yield()
The calling thread releases its remaining quantum upon calling this function.
Definition: Time.cpp:138
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:336
#define YARP_os_impl_API
Definition: api.h:46