Program Listing for File forkable.hpp

Return to documentation for file (rcppsw/multiprocess/forkable.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <sys/wait.h>
#include <unistd.h>

#include <string>
#include <thread>
#include <vector>

#include "rcppsw/rcppsw.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::multiprocess {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class forkable {
 public:
  forkable(void) = default;
  virtual ~forkable(void) = default;

  virtual void term(void) { m_proc_run = false; }

  virtual void proc_main(void) = 0;

  pid_t pid(void) const { return m_pid; }

  pid_t start(int core = -1);

  pid_t start(const std::string& new_wd, int core = -1);

 protected:
  bool terminated(void) const { return !m_proc_run; }

 private:
  static void entry_point(void* this_p) {
    auto pt = static_cast<forkable*>(this_p);
    pt->proc_main();
  }

  /* clang-format off */
  bool  m_proc_run{false};
  pid_t m_pid{0};
  /* clang-format on */
};

} /* namespace rcppsw::multiprocess */