1

I'm newbie here, so if I have any errors just tell me.

The problem is that I have two processes and I want them to execute concurrently because they take too much time. So I thought to implement a class timer which manage its own boost::asio::io_service and create a thread for this io_service. The code is the following:

timer.hpp

#include <iostream>
#include <string>
#include <functional>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

class timer
{
public:
    timer(std::function<void(void)> task,
          int time)
    : io__(),
      timer__(io__, boost::posix_time::milliseconds(time)),
      repetitive_task__(task),
      time_wait__(time)
    {
          timer__.async_wait(boost::bind(&timer::loop, this));
    }

    void start()
    {
        thread__ = std::thread([this](){ 
            io__.run(); 
        });
        thread__.join();
    }

    void loop()
    {
        repetitive_task__();
        timer__.expires_at(timer__.expires_at() + boost::posix_time::milliseconds(time_wait__));
        timer__.async_wait(boost::bind(&timer::loop, this));
    }

    void stop()
    {
        timer__.cancel();
        io__.stop();
    }

private:
    boost::asio::io_service io__;
    boost::asio::deadline_timer timer__;
    std::function<void(void)> repetitive_task__;
    int time_wait__;
    std::thread thread__;
};

For testing it, I have the simplest main I could think:

main.cpp

#include "timer.hpp"

void test1()
{
    printf("action1 \n");
}

void test2()
{
    printf("action 2 \n");
}

int main(int argc, char* argv[])
{
    timer timer1(&test1, 100);
    timer timer2(&test2, 50);

    timer1.start();
    timer2.start();
    return 0;
}

And the result is always action1. Never action2.

I've been looking for how to implement timers properly like in this post or in this example of boost, but I still don't understand what I am doing wrong.

Thanks in advance

Scarcha
  • 11
  • 1
  • 4
  • 1
    `thread__.join();` waits for the thread to exit. – tkausl Apr 25 '18 at 11:21
  • Thanks @tkausl. I'm checking other [posts](https://stackoverflow.com/questions/13999432/stdthread-terminate-called-without-an-active-exception-dont-want-to-joi). How do you think I could solve my problem? – Scarcha Apr 25 '18 at 12:07
  • Pass the `io_service` into the timer constructor as a reference so you only have one io_service in the program. Then call `io_service::poll()` or `io_service::run()` in the main thread. – Paul Belanger Apr 25 '18 at 12:25
  • Also, names with `__` are reserved for standard library implementations. Using them is not legal – sehe Apr 25 '18 at 14:01
  • thanks @sehe. I thought it was forbidden only at the beginning. – Scarcha Apr 25 '18 at 14:37
  • That too, for namespace/global symbols. But `__` is reserved anywhere. See https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – sehe Apr 25 '18 at 14:53

0 Answers0