Use cpputils threads that also work fine when fork()-ed
This commit is contained in:
parent
22a3c90d54
commit
52366fb707
35
implementations/caching/cache/PeriodicTask.cpp
vendored
35
implementations/caching/cache/PeriodicTask.cpp
vendored
@ -8,27 +8,24 @@ using namespace cpputils::logging;
|
|||||||
namespace blockstore {
|
namespace blockstore {
|
||||||
namespace caching {
|
namespace caching {
|
||||||
|
|
||||||
PeriodicTask::PeriodicTask(function<void ()> task, double intervalSec) : _thread(), _task(task), _intervalSec(intervalSec) {
|
PeriodicTask::PeriodicTask(function<void ()> task, double intervalSec) :
|
||||||
_thread = boost::thread([this]() {
|
_task(task),
|
||||||
boost::chrono::nanoseconds interval((uint64_t)(UINT64_C(1000000000) * _intervalSec));
|
_interval((uint64_t)(UINT64_C(1000000000) * intervalSec)),
|
||||||
try {
|
_thread(std::bind(&PeriodicTask::_loopIteration, this)) {
|
||||||
while(true) {
|
_thread.start();
|
||||||
boost::this_thread::sleep_for(interval);
|
|
||||||
_task();
|
|
||||||
}
|
|
||||||
} catch (const boost::thread_interrupted &e) {
|
|
||||||
//Do nothing, exit thread.
|
|
||||||
} catch (const std::exception &e) {
|
|
||||||
LOG(ERROR) << "PeriodicTask crashed: " << e.what();
|
|
||||||
} catch (...) {
|
|
||||||
LOG(ERROR) << "PeriodicTask crashed";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PeriodicTask::~PeriodicTask() {
|
void PeriodicTask::_loopIteration() {
|
||||||
_thread.interrupt();
|
try {
|
||||||
_thread.join();
|
boost::this_thread::sleep_for(_interval);
|
||||||
|
_task();
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
LOG(ERROR) << "PeriodicTask crashed: " << e.what();
|
||||||
|
throw;
|
||||||
|
} catch (...) {
|
||||||
|
LOG(ERROR) << "PeriodicTask crashed";
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
17
implementations/caching/cache/PeriodicTask.h
vendored
17
implementations/caching/cache/PeriodicTask.h
vendored
@ -3,20 +3,25 @@
|
|||||||
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_PERIODICTASK_H_
|
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING_CACHE_PERIODICTASK_H_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <boost/thread.hpp>
|
#include <messmer/cpp-utils/random/LoopThread.h>
|
||||||
|
#include <boost/chrono.hpp>
|
||||||
|
|
||||||
namespace blockstore {
|
namespace blockstore {
|
||||||
namespace caching {
|
namespace caching {
|
||||||
|
|
||||||
class PeriodicTask {
|
class PeriodicTask final {
|
||||||
public:
|
public:
|
||||||
PeriodicTask(std::function<void ()> task, double intervalSec);
|
PeriodicTask(std::function<void ()> task, double intervalSec);
|
||||||
virtual ~PeriodicTask();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::thread _thread;
|
void _loopIteration();
|
||||||
std::function<void ()> _task;
|
|
||||||
double _intervalSec;
|
std::function<void()> _task;
|
||||||
|
boost::chrono::nanoseconds _interval;
|
||||||
|
|
||||||
|
//This member has to be last, so the thread is destructed first. Otherwise the thread might access elements from a
|
||||||
|
//partly destructed PeriodicTask.
|
||||||
|
cpputils::LoopThread _thread;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user