Skip to content

Commit

Permalink
[Core] Fix: proper destructors / rule of 5 for everything involving s…
Browse files Browse the repository at this point in the history
…td::thread (#1334)
  • Loading branch information
KerstinKeller authored Jan 24, 2024
1 parent 16acfd2 commit a7f2c85
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
14 changes: 9 additions & 5 deletions ecal/core/include/ecal/ecal_timed_cb.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ namespace eCAL
**/
virtual ~CTimedCB() { Stop(); }

CTimedCB(const CTimedCB&) = delete;
CTimedCB& operator=(const CTimedCB&) = delete;
CTimedCB(CTimedCB&& rhs) = delete;
CTimedCB& operator=(CTimedCB&& rhs) = delete;

/**
* @brief Start the timer.
*
Expand Down Expand Up @@ -97,16 +102,15 @@ namespace eCAL
{
if (!m_running) return(false);
m_stop = true;
m_thread.join();
// Wait for the callback thread to finish
if (m_thread.joinable()) {
m_thread.join();
}
m_running = false;
return(true);
}

private:
// this object must not be copied.
CTimedCB(const CTimedCB&);
CTimedCB& operator=(const CTimedCB&);

void Thread(TimerCallbackT callback_, int timeout_, int delay_)
{
assert(callback_ != nullptr);
Expand Down
5 changes: 4 additions & 1 deletion ecal/core/src/io/shm/ecal_memfile_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ namespace eCAL
{
}

CMemFileThreadPool::~CMemFileThreadPool() = default;
CMemFileThreadPool::~CMemFileThreadPool()
{
Destroy();
}

void CMemFileThreadPool::Create()
{
Expand Down
5 changes: 5 additions & 0 deletions ecal/core/src/io/shm/ecal_memfile_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ namespace eCAL
CMemFileObserver();
~CMemFileObserver();

CMemFileObserver(const CMemFileObserver&) = delete;
CMemFileObserver& operator=(const CMemFileObserver&) = delete;
CMemFileObserver(CMemFileObserver&& rhs) = delete;
CMemFileObserver& operator=(CMemFileObserver&& rhs) = delete;

bool Create(const std::string& memfile_name_, const std::string& memfile_event_);
bool Destroy();

Expand Down
4 changes: 4 additions & 0 deletions ecal/core/src/time/ecal_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace eCAL
CTimerImpl(const int timeout_, TimerCallbackT callback_, const int delay_) : m_stop(false), m_running(false) { Start(timeout_, callback_, delay_); }

virtual ~CTimerImpl() { Stop(); }
CTimerImpl(const CTimerImpl&) = delete;
CTimerImpl& operator=(const CTimerImpl&) = delete;
CTimerImpl(CTimerImpl&& rhs) = delete;
CTimerImpl& operator=(CTimerImpl&& rhs) = delete;

bool Start(const int timeout_, TimerCallbackT callback_, const int delay_)
{
Expand Down
10 changes: 10 additions & 0 deletions ecal/core/src/util/ecal_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ namespace eCAL
CCallbackThread(std::function<void()> callback)
: callback_(callback) {}

~CCallbackThread()
{
stop();
}

CCallbackThread(const CCallbackThread&) = delete;
CCallbackThread& operator=(const CCallbackThread&) = delete;
CCallbackThread(CCallbackThread&& rhs) = delete;
CCallbackThread& operator=(CCallbackThread&& rhs) = delete;

/**
* @brief Start the callback thread with a specified timeout.
* @param timeout The timeout duration for waiting in the callback thread.
Expand Down

0 comments on commit a7f2c85

Please sign in to comment.