2015-10-16 02:46:17 +02:00
|
|
|
#pragma once
|
2015-11-07 09:00:25 +01:00
|
|
|
#ifndef MESSMER_CPPUTILS_THREAD_LOOPTHREAD_H
|
|
|
|
#define MESSMER_CPPUTILS_THREAD_LOOPTHREAD_H
|
2015-10-16 02:46:17 +02:00
|
|
|
|
2015-11-07 09:00:25 +01:00
|
|
|
#include "ThreadSystem.h"
|
|
|
|
#include <boost/optional.hpp>
|
2015-10-16 02:46:17 +02:00
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
//TODO Test
|
2015-10-29 15:51:05 +01:00
|
|
|
//TODO Test that fork() doesn't destroy anything (e.g. no deadlock on stop() because thread is not running anymore)
|
|
|
|
|
2015-10-28 15:00:24 +01:00
|
|
|
// Has to be final, because otherwise there could be a race condition where LoopThreadForkHandler calls a LoopThread
|
|
|
|
// where the child class destructor already ran.
|
|
|
|
class LoopThread final {
|
2015-10-16 02:46:17 +02:00
|
|
|
public:
|
2015-11-12 22:07:20 +01:00
|
|
|
// The loopIteration callback returns true, if more iterations should be run, and false, if the thread should be terminated.
|
|
|
|
LoopThread(std::function<bool()> loopIteration);
|
2015-10-28 15:00:24 +01:00
|
|
|
~LoopThread();
|
2015-10-16 02:46:17 +02:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
2015-11-12 22:07:20 +01:00
|
|
|
std::function<bool()> _loopIteration;
|
2015-11-07 09:00:25 +01:00
|
|
|
boost::optional<ThreadSystem::Handle> _runningHandle;
|
2015-10-16 02:46:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|