Program Listing for File threadable.hpp
↰ Return to documentation for file (rcppsw/multithread/threadable.hpp
)
#pragma once
/*******************************************************************************
* Includes
******************************************************************************/
#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include "rcppsw/rcppsw.hpp"
/*******************************************************************************
* Namespaces/Decls
******************************************************************************/
namespace rcppsw::multithread {
/*******************************************************************************
* Class Definitions
******************************************************************************/
class threadable {
public:
threadable(void) = default;
virtual ~threadable(void) = default;
threadable(const threadable&) = delete;
threadable& operator=(const threadable&) = delete;
virtual void* thread_main(void* arg) = 0;
status_t start(void* arg, int core = -1);
virtual void term(void) { m_thread_run = false; }
void join(void) { pthread_join(m_thread, nullptr); }
protected:
bool terminated(void) const { return m_thread_run; }
RCPPSW_DEAD void exit(void* ret = nullptr) {
m_thread_run = false;
if (nullptr == ret) {
int ret2;
pthread_exit(&ret2);
} else {
pthread_exit(ret);
}
}
pthread_t thread_handle(void) const { return m_thread; }
int64_t thread_id(void) const { return syscall(__NR_gettid); }
private:
static void* entry_point(void* this_p) {
auto* pt = static_cast<threadable*>(this_p);
return pt->thread_main(pt->m_arg);
} /* entry_point() */
/* clancg-format off */
bool m_thread_run{ false };
pthread_t m_thread{};
void* m_arg{ nullptr };
/* clancg-format on */
};
} /* namespace rcppsw::multithread */