io_service deadline timer does not work periodically - c++

i want to make a timer with 10s periodically in the class inside, but it does not work. It will print count in 10s at the first time. But after that, it does not wait another 10s. the specific code.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class test {
boost::asio::io_service& a;
boost::asio::deadline_timer t;
int count;
void print(const boost::system::error_code& /*e*/)
{
while(true)
{
std::cout << count << " ";
++(count);
t.expires_at(t.expires_at() + boost::posix_time::seconds(10));
t.async_wait(boost::bind(&test::print, this, boost::asio::placeholders::error));
}
}
public:
test(boost::asio::io_service& io) : a(io), t(io, boost::posix_time::seconds(10)) {
count = 0;
};
void start() {
t.async_wait(boost::bind(&test::print, this, boost::asio::placeholders::error));
}
};
int main()
{
boost::asio::io_service io;
int count = 0;
test b(io);
b.start();
io.run();
return 0;
}

Your print() function has an unnecessary while(true) loop. Once we remove that, we notice that the output doesn't come right away; that's because std::cout is line-buffered and we never write a newline. Here's the full fixed function:
void print(const boost::system::error_code& /*e*/)
{
std::cout << count << " " << std::flush;
++(count);
t.expires_at(t.expires_at() + boost::posix_time::seconds(10));
t.async_wait(boost::bind(&test::print, this, boost::asio::placeholders::error));
}

Related

Initializing boost::asio socket after constructor failed

I create a class to broadcast UDP messages as follows:
#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>
#include <string>
#include <queue>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using namespace std;
template<typename T>
std::string toString(const T& value);
std::string IntToString(const int& i);
class UdpCore
{
private:
boost::asio::ip::udp::endpoint endpoint;
boost::asio::ip::udp::socket socket;
string multicast_address;
unsigned short multicast_port;
boost::thread_group threads; // thread group
boost::thread* thread_main; // main thread
boost::thread* thread_listen; // listen thread
boost::thread* thread_getsend; // get/send thread
boost::mutex stopMutex;
bool initialize = false;
bool stop, showBroadcast;
int i_getsend, i_listen, i_main, i_message, interval;
string message;
public:
// constructor
UdpCore(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
: endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
socket(io_service, endpoint.protocol()),
multicast_address(multicast_address),
multicast_port(multicast_port),
interval(interval),
showBroadcast(show)
{
Initialize(io_service, show);
}
~UdpCore()
{
// show exit message
cout << "Exiting UDP Core." << endl;
}
// initialize
void Initialize(boost::asio::io_service& io_service, bool show = false)
{
if (initialize == false)
{
GetInfo();
}
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::make_address(multicast_address), multicast_port);
boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
socket.set_option(boost::asio::ip::udp::socket::reuse_address(true)); // no need
thread_main = new boost::thread(boost::ref(*this));
thread_getsend = new boost::thread(&UdpCore::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
threads.add_thread(thread_getsend); // get/send thread
stop = false;
showBroadcast = show;
i_getsend = 0;
i_listen = 0;
i_main = 0;
i_message = 0;
message.clear();
initialize = true;
}
void GetInfo()
{
multicast_address = "192.168.0.255";
multicast_port = 13000;
interval = 500;
}
// start the threads
void Start()
{
// Wait till they are finished
threads.join_all();
}
// stop the threads
void Stop()
{
// warning message
cout << "Stopping all threads." << endl;
// signal the threads to stop (thread-safe)
stopMutex.lock();
stop = true;
stopMutex.unlock();
// wait for the threads to finish
thread_main->interrupt(); // in case not interrupted by operator()
threads.interrupt_all();
threads.join_all();
// close socket after everything closes
socket.close();
}
void Callable_Listen(int interval, int& count)
{
while (!stop)
{
if (message != "")
socket.async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
{
stopMutex.lock();
if (showBroadcast)
{
cout << i_message << " - " << message << endl; // show count
}
message.clear(); //clear after sending
stopMutex.unlock();
});
++i_message;
// wait routine
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
++i_listen;
}
}
void Callable_GetSend(int interval, int& count, string& userInput)
{
while (!stop)
{
stopMutex.lock();
cout << "Callable_GetSend [" << count++ << "]. Enter message: ";
getline(cin, userInput);
if (message != "")
socket.async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
{
if (showBroadcast)
{
cout << i_message << " - " << message << endl; // show count
}
message.clear(); //clear after sending
});
stopMutex.unlock();
// wait routine
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
++i_getsend;
++i_message;
}
}
// Thread function
void operator () ()
{
while (!stop)
{
if (message == "STOP")
{
try
{
this->Stop();
}
catch (exception e)
{
cout << e.what() << endl;
}
}
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
}
}
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
std::string result = ctime(&now);
return result.erase(result.length() - 1, 1);
}
std::string some_string()
{
std::string result;
result = make_daytime_string();
return result;
}
};
int main()
{
try
{
boost::asio::io_service io_service;
UdpCore mt(io_service, "192.168.0.255", 13000, 5000, false);
mt.Start();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
It works fine. However, I wish to initialize the socket after the class constructor is initialized (to allow user input). Thus, I change from using socket to a socket pointer, as suggested by this post
. The modified code is as follows:
#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>
#include <string>
#include <queue>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using namespace std;
template<typename T>
std::string toString(const T& value);
std::string IntToString(const int& i);
class UdpCore
{
private:
boost::asio::ip::udp::endpoint endpoint;
boost::shared_ptr<udp::socket> socketPtr;
string multicast_address;
unsigned short multicast_port;
boost::thread_group threads; // thread group
boost::thread* thread_main; // main thread
boost::thread* thread_listen; // listen thread
boost::thread* thread_getsend; // get/send thread
boost::mutex stopMutex;
bool initialize = false;
bool stop, showBroadcast;
int i_getsend, i_listen, i_main, i_message, interval;
string message;
public:
// constructor
UdpCore(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
: multicast_address(multicast_address),
multicast_port(multicast_port),
interval(interval),
showBroadcast(show)
{
UdpCore(io_service, show);
}
UdpCore(boost::asio::io_service& io_service, bool show = false)
: showBroadcast(show)
{
Initialize(io_service, show);
}
// destructor
~UdpCore()
{
// show exit message
cout << "Exiting UDP Core." << endl;
}
// initialize
void Initialize(boost::asio::io_service& io_service, bool show = false)
{
if (initialize == false)
{
GetInfo();
}
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port);
socketPtr = boost::make_shared<udp::socket>(boost::ref(io_service), endpoint.protocol());
socketPtr->set_option(boost::asio::ip::udp::socket::reuse_address(true)); // no need
thread_main = new boost::thread(boost::ref(*this));
thread_getsend = new boost::thread(&UdpCore::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
threads.add_thread(thread_getsend); // get/send thread
stop = false;
showBroadcast = show;
i_getsend = 0;
i_listen = 0;
i_main = 0;
i_message = 0;
message.clear();
initialize = true;
}
void GetInfo()
{
multicast_address = "192.168.0.255";
multicast_port = 13000;
interval = 500;
}
// start the threads
void Start()
{
// Wait till they are finished
threads.join_all();
}
// stop the threads
void Stop()
{
// warning message
cout << "Stopping all threads." << endl;
// signal the threads to stop (thread-safe)
stopMutex.lock();
stop = true;
stopMutex.unlock();
// wait for the threads to finish
thread_main->interrupt(); // in case not interrupted by operator()
threads.interrupt_all();
threads.join_all();
// close socket after everything closes
socketPtr->close();
}
void Callable_Listen(int interval, int& count)
{
while (!stop)
{
if (message != "")
socketPtr->async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
{
stopMutex.lock();
if (showBroadcast)
{
cout << i_message << " - " << message << endl; // show count
}
message.clear(); //clear after sending
stopMutex.unlock();
});
++i_message;
// wait routine
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
++i_listen;
}
}
void Callable_GetSend(int interval, int& count, string& userInput)
{
while (!stop)
{
stopMutex.lock();
cout << "Callable_GetSend [" << count++ << "]. Enter message: ";
getline(cin, userInput);
if (message != "")
socketPtr->async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
{
if (showBroadcast)
{
cout << i_message << " - " << message << endl; // show count
}
message.clear(); //clear after sending
});
stopMutex.unlock();
// wait routine
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
++i_getsend;
++i_message;
}
}
// Thread function
void operator () ()
{
while (!stop)
{
if (message == "STOP")
{
try
{
this->Stop();
}
catch (exception e)
{
cout << e.what() << endl;
}
}
boost::this_thread::sleep(boost::posix_time::millisec(interval));
boost::this_thread::interruption_point();
}
}
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
std::string result = ctime(&now);
return result.erase(result.length() - 1, 1);
}
std::string some_string()
{
std::string result;
result = make_daytime_string();
return result;
}
};
int main()
{
try
{
boost::asio::io_service io_service;
UdpCore mt(io_service, false);
mt.Start();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
where I have these replacements:
//boost::asio::ip::udp::socket socket;
boost::shared_ptr<udp::socket> socketPtr;
//boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
//socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socketPtr = boost::make_shared<udp::socket>(boost::ref(io_service), endpoint.protocol());
socketPtr->set_option(boost::asio::ip::udp::socket::reuse_address(true));
//socket.async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
socketPtr->async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/)
Just one thing with the modification: it doesn't work. I keep poring over the code, and could not find the reason why it shouldn't work. Could someone please help?
In the first sample, besides the member endpoint and socket, you create a local endpoint and socket in Initialize() function. The latter objects are just unused - however, the member endpoint is properly initialized.
On the other hand, in the second example you do not properly initialize the member endpoint. Instead, you create again a local one. But pay attention that the endpoint used throughout the code is the member one.
The bottom line: remove the local objects and add correct initialization for the members.

Re-using Boost's timer objects

I modify the code from Boost's Timer 2 tutorial here
for a periodic printout:
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
static int i = 0;
i++;
std::cout << i << std::endl;
}
int main()
{
while(1)
{
boost::asio::io_context io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(&print);
t.wait();
io.run();
}
}
What I don't get is if I take the first two lines outside of while(1), it will not work properly. Is there a way to create these objects and reuse them? Thanks.
You need to schedule the next timer event with
expires_at http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/basic_deadline_timer/expires_at.html
or expires_from_now http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/basic_deadline_timer/expires_from_now.html
So, like:
#include <boost/asio.hpp>
#include <iostream>
void print(boost::system::error_code ec) {
static int i = 0;
if (ec != boost::asio::error::operation_aborted) {
i++;
}
std::cout << i << " (" << ec.message() << ")" << std::endl;
}
int main() {
boost::asio::io_service io;
boost::asio::deadline_timer t(io);
while (1) {
t.expires_from_now(boost::posix_time::seconds(1));
t.async_wait(&print);
if (io.stopped()) { io.reset(); }
io.run();
}
}
OK, that part is apparently in Timer 3 tutorial. Also, the documentations of 1.66 has missing info. Gotta change to 1.65 to see it.

Boost timer expires immediately

I run boost deadline_timer and do async_wait, but timer cancelled immediately. What i am doing wrong? I run ioService in my main file.
Thank you for any possible help
class A(boost::asio:io_service& ioService):
m_timer(ioService)
{
m_timer.expires_at(boost::posix_time::pos_infin);
m_timer.async_wait([this](const boost::system::error_code& ec)
{
std::cout << "Timer callback " << ec.message() << std::endl;
});
Check the lifetime of your A object.
E.g. if you do this:
#include <boost/asio.hpp>
#include <iostream>
struct A {
A(boost::asio::io_service& ioService) : m_timer(ioService)
{
m_timer.expires_at(boost::posix_time::pos_infin);
m_timer.async_wait(
[this](const boost::system::error_code& ec) { std::cout << "Timer callback " << ec.message() << std::endl; }
);
}
boost::asio::deadline_timer m_timer;
};
int main()
{
boost::asio::io_service svc;
{
A a(svc);
}
svc.run();
}
The timer will have been canceled even before run() is invoked.
The following will do what you expected
int main()
{
boost::asio::io_service svc;
{
A a(svc);
svc.run();
} // A destructed after `run()` completes
}

boost asio deadline_timer async_wait(N seconds) twice within N seconds cause operation canceled

What I want is when one message queue receives an int N, the handler function will be called after N seconds. below is my code.
It runs OK if the duration seconds of two near message queue is larger than the int N, but the handler will print "Operation canceled" in one handler when the duration seconds between two received message queues are smaller than N, which is not what I want.
I'd appreciate a lot for any help.
#include <boost/asio.hpp>
#include <zmq.h>
#include <boost/thread.hpp>
#include <iostream>
boost::asio::io_service io_service;
void* context = zmq_ctx_new();
void* sock_pull = zmq_socket(context, ZMQ_PULL);
void handler(const boost::system::error_code &ec) {
std::cout << "hello, world" << "\t" << ec.message() << std::endl;
}
void run() {
io_service.run();
}
void thread_listener() {
int nRecv;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(0));
while( true ) {
zmq_recv(sock_pull, &nRecv, sizeof(nRecv), 0);
std::cout << nRecv << std::endl;
timer.expires_from_now(boost::posix_time::seconds(nRecv));
timer.async_wait(handler);
}
}
int main(int argc, char* argv[]) {
boost::asio::io_service::work work(io_service);
zmq_bind(sock_pull, "tcp://*:60000");
boost::thread tThread(thread_listener);
boost::thread tThreadRun(run);
tThread.join();
tThreadRun.join();
return 0;
}
When you call
timer.expires_from_now(boost::posix_time::seconds(nRecv));
this, as the documentation states, cancels any async timer pending.
If you want to have overlapping requests in flight at a given time, one timer is clearly not enough. Luckily there is a wellknown pattern around bound shared pointers in Asio that you can use to mimick a "session" per response.
Say you define a session to contain it's own private timer:
struct session : boost::enable_shared_from_this<session> {
session(boost::asio::io_service& svc, int N) :
timer(svc, boost::posix_time::seconds(N))
{
// Note: shared_from_this is not allowed from ctor
}
void start() {
// it's critical that the completion handler is bound to a shared
// pointer so the handler keeps the session alive:
timer.async_wait(boost::bind(&session::handler, shared_from_this(), boost::asio::placeholders::error));
}
private:
void handler(const boost::system::error_code &ec) {
std::cout << "hello, world" << "\t" << ec.message() << std::endl;
}
boost::asio::deadline_timer timer;
};
Now, it's trivial to replace the code that used the hardcoded timer instance:
timer.expires_from_now(boost::posix_time::seconds(nRecv));
timer.async_wait(handler);
with the session start:
boost::make_shared<session>(io_service, nRecv)->start();
A fully working example (with suitably stubbed ZMQ stuff): Live On Coliru
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
boost::asio::io_service io_service;
/////////////////////////////////////////////////////////////////////////
// I love stubbing out stuff I don't want to install just to help others
enum { ZMQ_PULL };
static void* zmq_ctx_new() { return nullptr; }
static void* zmq_socket(void*,int) { return nullptr; }
static void zmq_bind(void*,char const*) {}
static void zmq_recv(void*,int*data,size_t,int)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(rand()%1000));
*data = 2;
}
// End of stubs :)
/////////////////////////////////////////////////////////////////////////
void* context = zmq_ctx_new();
void* sock_pull = zmq_socket(context, ZMQ_PULL);
struct session : boost::enable_shared_from_this<session> {
session(boost::asio::io_service& svc, int N) :
timer(svc, boost::posix_time::seconds(N))
{
// Note: shared_from_this is not allowed from ctor
}
void start() {
// it's critical that the completion handler is bound to a shared
// pointer so the handler keeps the session alive:
timer.async_wait(boost::bind(&session::handler, shared_from_this(), boost::asio::placeholders::error));
}
~session() {
std::cout << "bye (session end)\n";
}
private:
void handler(const boost::system::error_code &ec) {
std::cout << "hello, world" << "\t" << ec.message() << std::endl;
}
boost::asio::deadline_timer timer;
};
void run() {
io_service.run();
}
void thread_listener() {
int nRecv = 0;
for(int n=0; n<4; ++n) {
zmq_recv(sock_pull, &nRecv, sizeof(nRecv), 0);
std::cout << nRecv << std::endl;
boost::make_shared<session>(io_service, nRecv)->start();
}
}
int main() {
auto work = boost::make_shared<boost::asio::io_service::work>(io_service);
zmq_bind(sock_pull, "tcp://*:60000");
boost::thread tThread(thread_listener);
boost::thread tThreadRun(run);
tThread.join();
work.reset();
tThreadRun.join();
}

how can it do other work except "async_wait" in one single thread using boost.asio.deadline_timer

I rewrite a boost.asio example "Timer.3 - Binding arguments to a handler". I think "worker" can work forever and "print" can be called each second. But "print" only is called once. It is very diffcult for me to think why.
If this can't work,how can I do other things except "async_wait" in one single thread.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int num = 0;
void worker(){
while(1){
++num;
}
}
void print(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t, int* count)
{
std::cout << *count << "\n";
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(
print, boost::asio::placeholders::error, t, count
));
if(*count == 1){
worker();
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print,
boost::asio::placeholders::error, &t, &count
));
io.run();
std::cout << "Final count is " << count << "\n";
return 0;
}
You have an infinite loop
void worker() {
while(1) {
++num;
}
}
so control never returns the first time print() is invoked.