Go to the documentation of this file.
5 #ifndef CCF_MPTK_MANAGER_HPP_
6 #define CCF_MPTK_MANAGER_HPP_
8 #include <core/utils/system/system.hpp>
55 template <
class THREAD_TYPE>
57 const double period = 0.1,
58 THREAD_TYPE** thread_obj =
nullptr) {
61 BEGIN_CRIT_SEC(m_mgr_id) {
62 m_threads[thread_id] = std::make_shared<THREAD_TYPE>(thread_id,
MsgBus(), period);
63 if (m_threads[thread_id].get() ==
nullptr) {
64 throw rad::Exception(fmt::format(
"Could not create thread with ID: %s in MPTK instance", thread_id.c_str()));
68 if (thread_obj !=
nullptr) {
69 *thread_obj =
static_cast<THREAD_TYPE*
>(m_threads[thread_id].get());
75 template <
class THREAD_TYPE>
77 const std::string& thread_id,
78 const double period = 0.1,
79 THREAD_TYPE** thread_obj =
nullptr) {
82 BEGIN_CRIT_SEC(m_mgr_id) {
83 m_threads[thread_id] = std::make_shared<THREAD_TYPE>(thread_id,
MsgBus(), period);
84 if (m_threads[thread_id].get() ==
nullptr) {
85 throw rad::Exception(fmt::format(
"Could not create thread with ID: %s in MPTK instance", thread_id.c_str()));
89 if (thread_obj !=
nullptr) {
90 *thread_obj =
static_cast<THREAD_TYPE*
>(m_threads[thread_id].get());
95 template <
class THREAD_TYPE>
97 THREAD_TYPE** thread_obj) {
100 core::utils::system::Mutex tmp_mutex(m_mgr_id); {
102 auto thr_obj_it = m_threads.find(thread_id);
103 if (thr_obj_it == m_threads.end()) {
104 throw rad::Exception(fmt::format(
"%s: Undefined Thread ID: %s",
105 m_mgr_id.c_str(), thread_id.c_str()));
107 *thread_obj =
static_cast<THREAD_TYPE*
>(thr_obj_it->second.get());
113 void GetThreadIds(std::list<std::string>& thread_ids)
const;
116 std::string m_mgr_id;
118 std::map<std::string, std::shared_ptr<Thread>> m_threads;
123 #endif // CCF_MPTK_MANAGER_HPP_
MessageBus & RegisterThread(const std::string &thread_id)
Register thread which will send/receive messages on the Message Bus.
Definition: messageBus.cpp:64
~Manager()
Definition: manager.cpp:17
Definition: manager.hpp:15
void Reset()
Reset the object. This includes stopping the possible running threads and deleting all thread objects...
Definition: manager.cpp:22
MessageBus & MsgBus()
Get reference to Message Bus.
Definition: manager.cpp:75
void GetThread(const std::string &thread_id, THREAD_TYPE **thread_obj)
Get reference to MPTK thread, registered in the Thread Registry.
Definition: manager.hpp:96
IFW CTD Multiprocessing Toolkit Manager class.
Definition: manager.hpp:21
void PauseThreads()
Pause the threads.
Definition: manager.cpp:61
void GetThreadIds(std::list< std::string > &thread_ids) const
Return list with IDs of threads registered.
Definition: manager.cpp:80
IFW CTD Multiprocessing Toolkit Message Bus.
Definition: messageBus.hpp:30
void CreateThread(const std::string &thread_id, const double period=0.1, THREAD_TYPE **thread_obj=nullptr)
Allocate new MPTK thread object of the given type. The allocated thread object is handled internally ...
Definition: manager.hpp:56
int16_t NbOfThreadsRunning() const
Returns the number of threads running; this includes the ones that may be paused.
Definition: manager.cpp:52
std::string ToString() const
Generate ASCII output providing a status of the object.
void StopThreads()
Request threads to end execution.
Definition: manager.cpp:68
Manager()
Definition: manager.cpp:12
int16_t WaitForThreadTerm(const double time_out=core::utils::base::NO_TIMEOUT)
Wait for threads to terminate execution. Returns the number of threads running.
Definition: manager.cpp:37
void StartThreads()
Execute all threads registered.
Definition: manager.cpp:30
void CreateThread(const THREAD_TYPE &factory_object, const std::string &thread_id, const double period=0.1, THREAD_TYPE **thread_obj=nullptr)
Allocate new MPTK thread object of the given type. The allocated thread object is handled internally ...
Definition: manager.hpp:76