I want to implement a java-like Timer by asio's timer, it used to execute code periodically.
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print()
{
std::cout << "Hello, world!\n";
}
class WorldTimer
{
public:
boost::posix_time::ptime now()
{
return _timer.expires_at();
}
void update()
{
_f();
_timer.expires_at(_timer.expires_at() + boost::posix_time::milliseconds(1000));
_timer.async_wait(boost::bind(&WorldTimer::update, this));
}
WorldTimer(boost::asio::io_service& io, void (*f)()) : _f(f), _timer(io){}
private:
void (*_f)();
boost::asio::deadline_timer _timer;
};
int main() {
boost::asio::io_service io;
WorldTimer timer(io, print);
timer.update();
io.run();
return 0;
}
Program only output Hello, world! once and pending there. asio doc has a example, it works but I can not figure out what's the difference.
Yeah... timer has not been init a expire time, this is revised version:
class WorldTimer
{
public:
boost::posix_time::ptime now()
{
return _timer.expires_at();
}
WorldTimer(boost::asio::io_service& io, void (*f)()) : _f(f), _timer(io, boost::posix_time::microseconds(0))
{
_timer.async_wait(boost::bind(&WorldTimer::update, this));
}
private:
void (*_f)();
boost::asio::deadline_timer _timer;
void update()
{
_f();
_timer.expires_at(_timer.expires_at() + boost::posix_time::milliseconds(1000));
_timer.async_wait(boost::bind(&WorldTimer::update, this));
}
};
int main() {
boost::asio::io_service io;
WorldTimer timer(io, print);
io.run();
return 0;
}
Your deadline timer constructor is different from the one in the example. You need to explicitly set the expiry time.
The example code uses the other constructor which sets a particular expiry time relative to now.
So the print-out you are seeing is related to your call to update, which calls
_timer.expires_at(_timer.expires_at() + boost::posix_time::milliseconds(1000));
and _timer.expires_at() has not been set yet...
Related
I'm working on a timer class to perform operations on a different thread, sample code below is a copy from another SO question HERE
#include <thread>
#include <chrono>
#include <functional>
class Timer
{
public:
~Timer();
Timer() noexcept;
typedef std::chrono::milliseconds Interval;
typedef std::function<void(void)> Timeout;
public:
void start(const Interval& interval, const Timeout& timeout);
void stop();
private:
std::thread mThread; /** Timer thread */
bool mRunning = false; /** Timer status */
};
Implementation with a comment where the problem will occur:
Timer::~Timer()
{
}
Timer::Timer() noexcept
{
}
void Timer::start(const Interval& interval, const Timeout& timeout)
{
mRunning = true;
mThread = std::thread([&]()
{
while (mRunning == true)
{
std::this_thread::sleep_for(interval);
// std::abort will be called here
timeout();
}
});
}
void Timer::stop()
{
mRunning = false;
mThread.join();
}
Sample to test the timer:
#include <iostream>
int main()
{
Timer tm;
tm.start(std::chrono::milliseconds(1000), []
{
std::cout << "Hello!" << std::endl;
});
std::this_thread::sleep_for(std::chrono::seconds(4));
tm.stop();
}
I'm not able to understand why std::abort is called while executing the std::function within lambda and how do I resolve this?
Arguments to your start function are passed by reference. In your lambda, you capture them by reference. By the time you come around calling that lambda, everything you've captured is destroyed, thus you're causing undefined behavior.
Additionally, make sure to either use atomic<bool> instead of a regular bool:
#include <thread>
#include <chrono>
#include <functional>
#include <cstdio>
#include <atomic>
class Timer {
public:
~Timer() {
if (mRunning) {
stop();
}
}
typedef std::chrono::milliseconds Interval;
typedef std::function<void(void)> Timeout;
void start(const Interval &interval, const Timeout &timeout) {
mRunning = true;
mThread = std::thread([this, interval, timeout] {
while (mRunning) {
std::this_thread::sleep_for(interval);
timeout();
}
});
}
void stop() {
mRunning = false;
mThread.join();
}
private:
std::thread mThread{};
std::atomic_bool mRunning{};
};
int main() {
Timer tm;
tm.start(std::chrono::milliseconds(1000), [] {
std::puts("Hello!");
});
std::this_thread::sleep_for(std::chrono::seconds(4));
}
P.S. you might want to look into coroutines depending on where this idea is going.
As title says I'm having this problem when I can't understand why this thread
never stops running even after Client's destructor destroys asio::io_service::work variable
When I'm running this program output is always like this
Anyone sees what I'm missing here?
#include <boost/asio.hpp>
#include <thread>
#include <atomic>
#include <memory>
using namespace boost;
class Client{
public:
Client(const Client& other) = delete;
Client()
{
m_work.reset(new boost::asio::io_service::work(m_ios));
m_thread.reset(new std::thread([this]()
{
m_ios.run();
}));
}
~Client(){ close(); }
private:
void close();
asio::io_service m_ios;
std::unique_ptr<boost::asio::io_service::work> m_work;
std::unique_ptr<std::thread> m_thread;
};
void Client::close()
{
m_work.reset(nullptr);
if(m_thread->joinable())
{
std::cout << "before joining thread" << std::endl;
m_thread->join();
std::cout << "after joining thread" << std::endl;
}
}
int main()
{
{
Client client;
}
return 0;
}
EDIT
After StPiere comments I changed code to this and it worked :)
class Client{
public:
Client(const Client& other) = delete;
Client()
{
m_work.reset(new boost::asio::executor_work_guard<boost::asio::io_context::executor_type>(boost::asio::make_work_guard(m_ios)));
m_thread.reset(new std::thread([this]()
{
m_ios.run();
}));
}
~Client(){ close(); }
private:
void close();
asio::io_context m_ios;
std::unique_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> m_work;
std::unique_ptr<std::thread> m_thread;
};
I cannot reproduce the error on either compiler.
Here example for gcc 9.3 and boost 1.73
Normally the work destructor will use something like InterLockedDecrement on windows to decrement the number of outstanding works.
It looks like some compiler or io_service/work implementation issue.
As stated in comments, io_service and io_service::work are deprecated in terms of io_context and executor_work_guard.
Background
I am trying to stop periodic tasks when user interrupts process with SIGINT. I have based my periodic task scheduler on this answer.
To accomplish this I tried passing PeriodicScheduler instance pointer to my InterruptHandler and calling ps->stop().
Periodic Task Scheduler header:
#ifndef __PERIODICSCHEDULER_H__
#define __PERIODICSCHEDULER_H__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
namespace APP{
class PeriodicTask : boost::noncopyable {
public:
typedef std::function<void()> handler_fn;
PeriodicTask(boost::asio::io_service& ioService
, std::string const& name
, int interval
, handler_fn task);
void execute(boost::system::error_code const& e);
void start();
private:
void start_wait();
boost::asio::io_service& ioService;
boost::asio::deadline_timer timer;
handler_fn task;
std::string name;
int interval;
}; /* class PeriodicTask */
class PeriodicScheduler : boost::noncopyable
{
public:
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
void run();
void stop();
void addTask(std::string const& name
, PeriodicTask::handler_fn const& task
, int interval);
private:
boost::asio::io_service io_service;
std::vector<std::unique_ptr<PeriodicTask>> tasks;
}; /* PeriodicScheduler */
} /* namespace Resto */
#endif /* __PERIODICSCHEDULER_H__ */
Periodic Task Scheduler source:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include "periodicScheduler.h"
APP::PeriodicTask::PeriodicTask(boost::asio::io_service& ioService
, std::string const& name
, int interval
, handler_fn task)
: ioService(ioService)
, interval(interval)
, task(task)
, name(name)
, timer(ioService){
// Schedule start to be ran by the io_service
ioService.post(boost::bind(&PeriodicTask::start, this));
}
void APP::PeriodicTask::execute(boost::system::error_code const& e){
if (e != boost::asio::error::operation_aborted) {
task();
timer.expires_at(timer.expires_at() + boost::posix_time::seconds(interval));
start_wait();
}
}
void APP::PeriodicTask::start(){
// Uncomment if you want to call the handler on startup (i.e. at time 0)
// task();
timer.expires_from_now(boost::posix_time::seconds(interval));
start_wait();
}
void APP::PeriodicTask::start_wait(){
timer.async_wait(boost::bind(&PeriodicTask::execute
, this
, boost::asio::placeholders::error));
}
void APP::PeriodicScheduler::run(){
io_service.run();
}
void APP::PeriodicScheduler::stop(){
io_service.stop();
}
void APP::PeriodicScheduler::addTask(std::string const& name
, PeriodicTask::handler_fn const& task
, int interval){
tasks.push_back(make_unique<PeriodicTask>(std::ref(io_service)
, name, interval, task));
}
The following is InterruptHandler:
#include <csignal>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <boost/asio.hpp>
#include "periodicScheduler.h"
static std::condition_variable _condition;
static std::mutex _mutex;
namespace APP {
class InterruptHandler {
public:
static void hookSIGINT() {
signal(SIGINT, handleUserInterrupt);
}
static void handleUserInterrupt(int signal){
if (signal == SIGINT) {
std::cout << "SIGINT trapped ..." << '\n';
_condition.notify_one();
}
}
static void waitForUserInterrupt(APP::PeriodicScheduler *ps) {
std::unique_lock<std::mutex> lock { _mutex };
_condition.wait(lock);
ps->stop();
std::cout << "user has signaled to interrup program..." << '\n';
lock.unlock();
}
};
}
My main()
int main(int ac, const char * av[]) {
InterruptHandler::hookSIGINT();
APP::PeriodicScheduler ps;
APP::WorkerClass wc;
// WorkerClass::someTask and WorkerClass:someOtherTask are dummy functions only with sleep(5); inside them
ps.addTask("someTask", boost::bind( &APP::WorkerClass::someTask, wc ), 60);
ps.addTask("someOtherTask", boost::bind( &APP::WorkerClass::someOtherTask, wc ), 60);
ps.run();
InterruptHandler::waitForUserInterrupt(&ps);
return 0;
}
Issue
After running my app in terminal I pressed CTRL+C to trigger interrupt. I can see SIGINT trapped ... in the terminal but, application continues to run.
If I comment out ps.run(); statement, upon pressing CTRL+C I can see SIGINT trapped ... followed by user has signaled to interrup program... and application exits.
Questions
Is my approach correct? How can I effectively stop scheduled tasks and exit application?
Did I miss something?
By all means, I'd suggest using signal_set https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/signal_set.html
Here are some examples: https://stackoverflow.com/search?q=user%3A85371+signal_set
The best part is that is insulates you from some platform specific things and removes common pitfalls related to writing async-safe handlers.
I'm trying to save the result of bind to std:function, then pass it as parameter to another function, and store it as data member. Then I use asio async_wait, but when i return from the wait, and try to operate the function i saved i get segmentation fault. any Idea why?
#include <memory>
#include <iostream>
#include <asio/io_service.hpp>
#include <functional>
#include <asio/deadline_timer.hpp>
using namespace std;
typedef std::function<void (const std::error_code& error)> TM_callback;
class Timer {
public:
Timer(asio::io_service& io_service) :_timer(io_service) {}
void start(TM_callback cb) {
_cb = cb;
_timer.expires_from_now(boost::posix_time::milliseconds(1000));
TM_callback timeoutFunc = std::bind(&Timer::onTimeout, this, std::placeholders::_1);
_timer.async_wait(timeoutFunc);
}
private:
void onTimeout(const std::error_code& error) {
(_cb)(error); // <-- here i get segmentation fault
}
TM_callback _cb;
asio::deadline_timer _timer;
};
class COL {
public:
COL(asio::io_service& io_service): _inTimer(io_service){}
void startInTimer() {
TM_callback cb = std::bind(&COL::onInTimeout, this, std::placeholders::_1);
_inTimer.start(cb);
}
private:
void onInTimeout(const std::error_code& error) {cout<<error.message();}
Timer _inTimer;
};
int main()
{
asio::io_service io_service;
COL col(io_service);
col.startInTimer();
return 0;
}
Ok, the most likely problem is in the code you don't show. As you can see #m.s. didn't "imagine" your problem. He forgot the io_service::run() too:
int main() {
asio::io_service io_service;
COL col(io_service);
col.startInTimer();
io_service.run();
}
Still no problem. Live On Coliru
The problem starts when inTimer is not guaranteed to live until the completion handler is executed:
int main() {
asio::io_service io_service;
{
COL col(io_service);
col.startInTimer();
}
io_service.run();
}
Now you have Undefined Behaviour: Live On Coliru
Solution
The easiest solution is to make the COL (what is that?) object live long enough. The more structural/idiomatic way would to let the bind keep the Timer object alive, e.g. using a shared_ptr:
Live On Coliru
#include <iostream>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/asio.hpp>
using namespace std;
typedef std::function<void(const boost::system::error_code &error)> TM_callback;
namespace asio = boost::asio;
class Timer : public boost::enable_shared_from_this<Timer> {
public:
Timer(asio::io_service &io_service) : _timer(io_service) {}
void start(TM_callback cb) {
_cb = cb;
_timer.expires_from_now(boost::posix_time::milliseconds(1000));
TM_callback timeoutFunc = boost::bind(&Timer::onTimeout, shared_from_this(), boost::asio::placeholders::error);
_timer.async_wait(timeoutFunc);
}
private:
void onTimeout(const boost::system::error_code &error) {
(_cb)(error);
}
TM_callback _cb;
asio::deadline_timer _timer;
};
class COL : public boost::enable_shared_from_this<COL> {
public:
COL(asio::io_service &io_service) : _svc(io_service) {}
void startInTimer() {
TM_callback cb = boost::bind(&COL::onInTimeout, shared_from_this(), boost::asio::placeholders::error);
boost::shared_ptr<Timer> _inTimer = boost::make_shared<Timer>(_svc);
_inTimer->start(cb);
}
private:
void onInTimeout(const boost::system::error_code &error) { cout << error.message(); }
asio::io_service& _svc;
};
int main() {
asio::io_service io_service;
{
boost::make_shared<COL>(io_service)->startInTimer();
}
io_service.run();
}
Note that this subtly also fixes the problem that more than one timer couldn't be in flight at a give time (scheduling a new timer would cancel the pending one).
I have the following code:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <map>
#include <utility>
namespace koicxx {
template <typename T>
class temp_storage : private boost::noncopyable
{
typedef boost::shared_ptr<boost::asio::deadline_timer> shared_timer_t;
typedef std::map<T, shared_timer_t> timer_map_t;
typedef std::pair<T, shared_timer_t> timer_pair_t;
typedef boost::function<void(const T&, const boost::system::error_code&)> callback_t;
public:
temp_storage(boost::asio::io_service& io_service) :
_io_service(io_service) {}
bool add(const T& element, const boost::asio::deadline_timer::duration_type& timeout, callback_t callback = callback_t())
{
boost::lock_guard<boost::mutex> lock(_sync);
const std::pair<timer_map_t::iterator, bool>& res =
_internal_storage.insert(
timer_pair_t(
element
, shared_timer_t(new boost::asio::deadline_timer(_io_service, timeout))
));
if (!res.second)
{
return false;
}
const timer_map_t::iterator& itr = res.first;
if (callback)
{
itr->second->async_wait(
boost::bind(
callback
, itr->first
, boost::asio::placeholders::error
));
}
itr->second->async_wait(
boost::bind(
&temp_storage::remove_callback
, this
, itr->first
, boost::asio::placeholders::error
));
return true;
}
bool remove(const T& element)
{
boost::lock_guard<boost::mutex> lock(_sync);
const timer_map_t::iterator& itr = _internal_storage.find(element);
if (itr == _internal_storage.end())
{
return false;
}
itr->second->cancel();
_internal_storage.erase(itr);
return true;
}
bool contains(const T& element)
{
boost::lock_guard<boost::mutex> lock(_sync);
return _internal_storage.find(element) != _internal_storage.end();
}
void clear()
{
boost::lock_guard<boost::mutex> lock(_sync);
for (timer_map_t::value_type& i : _internal_storage)
{
i.second->cancel();
}
_internal_storage.clear();
}
private:
void remove_callback(const T& element, const boost::system::error_code& e)
{
if (e == boost::asio::error::operation_aborted)
{
return;
}
remove(element);
}
boost::asio::io_service& _io_service;
timer_map_t _internal_storage;
boost::mutex _sync;
};
} // namespace koicxx
int main()
{
boost::asio::io_service io_service;
koicxx::temp_storage<int> some_storage(io_service);
some_storage.add(0, boost::posix_time::seconds(2));
some_storage.add(1, boost::posix_time::seconds(3));
some_storage.add(2, boost::posix_time::seconds(5));
while (true)
{
if (some_storage.contains(0))
{
std::cout << 0 << ' ';
}
if (some_storage.contains(1))
{
std::cout << 1 << ' ';
}
if (some_storage.contains(2))
{
std::cout << 2 << ' ';
}
std::cout << '\n';
boost::this_thread::sleep_for(boost::chrono::seconds(1));
}
}
When I need to run io_service and why? Could I make io_service member of the class? Is there smth wrong with this code?
Thanks in advance.
You never see your timers expire.
When calling async_wait what you are telling Asio is this: When the timer expires, I want you to schedule this callback for execution. Note that 'schedule' here does not mean 'execute immediately', but rather 'insert it into a queue of stuff that is ready for execution'. Said queue is part of io_service's internals. Calling run on io_service will block until all pending work has been scheduled and executed.
The problem here is that run waits for both callbacks that have been scheduled (ie. those that are already ready for execution) and those that are still waiting to be scheduled (ie. those where you have called async_wait but where the timer has not expired yet). So just calling run from the main thread will simply block until all three of your timers have expired, which is probably not what you want.
You have two options now: You can either open a second thread to call run. This would work, but you would end up with two threads mostly doing nothing (the main thread, which is mainly sleeping in the loop, and the worker thread mainly sleeping on the run call).
A more lightweight approach is to call poll instead from the loop. Unlike run, poll only exeuctes callbacks that have been scheduled for execution already, but not those that are still waiting. If no such callbacks are available, poll returns immediately instead of blocking:
template <typename T>
class temp_storage : private boost::noncopyable
{
public:
void do_poll() {
io_service_.poll();
}
[...]
};
int main()
{
[...]
while (true)
{
[...]
some_storage.do_poll();
boost::this_thread::sleep_for(boost::chrono::seconds(1));
}
}