Skip to content
Snippets Groups Projects
Commit a74b17ac authored by Jeremie Deray's avatar Jeremie Deray
Browse files

add ThreadedBaseClass

parent 7be3f4ec
No related branches found
No related tags found
1 merge request!90[WIP] ProcessorBase multi-threading
#include "threaded_base_class.h"
namespace wolf
{
namespace core
{
ThreadedBaseClass::ThreadedBaseClass(unsigned int max_frequency) :
run_(false),
period_(1000/max_frequency)
{
//
}
ThreadedBaseClass::~ThreadedBaseClass()
{
if ( run_.load(std::memory_order_acquire) ) stop();
}
void ThreadedBaseClass::run()
{
if ( run_.load(std::memory_order_acquire) ) return;
run_.store(true, std::memory_order_release);
thread_ = std::thread(&ThreadedBaseClass::execute, this);
//_future = std::async(std::launch::async, &Talker::execute, this);
}
void ThreadedBaseClass::stop()
{
run_.store(false, std::memory_order_release);
cv_.notify_one();
if ( thread_.joinable() ) thread_.join();
}
bool ThreadedBaseClass::isRunning()
{
return run_.load(std::memory_order_acquire);
}
void ThreadedBaseClass::execute()
{
while ( run_.load(std::memory_order_acquire) )
{
auto start = std::chrono::system_clock::now();
executeImpl();
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<ms>(end-start);
auto diff = period_ - elapsed;
// @Todo warn if diff < 0.
// meaning that the process is actually slower
// than the user defined period (max_frequency).
diff = (diff >= ms(0))? diff : ms(0);
sleep(diff);
}
}
} // namespace core
} // namespace wolf
/**
* \file threaded_class.h
* \brief A base class for self-threaded object
* \author Jeremie Deray
* Created on: 26/09/2016
*/
#ifndef WOLF_THREADED_CLASS_BASE_H_
#define WOLF_THREADED_CLASS_BASE_H_
#include <thread>
#include <mutex>
#include <iostream>
#include <future>
namespace wolf
{
namespace core
{
template<typename _Rep, typename _Period>
void sleep(const std::chrono::duration<_Rep, _Period>& dur)
{
std::this_thread::sleep_for(
std::forward<const std::chrono::duration<_Rep, _Period> >(dur));
}
class ThreadedBaseClass
{
using ms = std::chrono::milliseconds;
public:
/**
* \brief ThreadedClass Base class that wraps an 'execute' function in a thread.
*
* \param name An identification name.
*
* \param max_frequency The maximum frequency at which the thread should run in Hz.
* It might actually be slower depending on the load of 'executeImpl'.
*
* \see executeImpl
**/
ThreadedBaseClass(unsigned int max_frequency);
virtual ~ThreadedBaseClass();
/**
* \brief Starts Main loop.
* Creates thread in which 'execute' is called.
**/
virtual void run();
/**
* \brief Main loop.
**/
virtual void stop();
bool isRunning();
protected:
std::atomic<bool> run_;
std::condition_variable cv_;
/** \brief Max period of the execution loop in nanosec */
ms period_;
std::thread thread_;
//std::future<DataPtr> _future;
/**
* \brief execute. Main loop.
**/
virtual void execute();
/**
* \brief Derived has to provide this function.
**/
virtual void executeImpl() = 0;
};
} // namespace core
} // namespace wolf
#endif /* WOLF_THREADED_CLASS_BASE_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment