Boost logo

Boost Users :

Subject: [Boost-users] [asio] 1.37.0: trouble with multiple timers and serial line port
From: Markus Werle (numerical.simulation_at_[hidden])
Date: 2009-03-11 08:58:57


Hi!

Using boost-1.37.0
Having some trouble with multiple timers in one class.

Looks like the timers are going berserk when
MemFun1() calls Process(): The expiration wait times then are
ignored and the whole loop seesm to run as if timers are
all set to 0. Any idea what went wrong?

A little code sketch (does not compile)

class Communicator
{
        // the main IO service that runs this connection
         boost::asio::io_service io_service_;

        // the serial port this instance is connected to
        boost::asio::serial_port SerialPort_;

        // maximum amount of data to read in one operation
        static const int max_read_length = 256;

        // data read from the socket
        char read_msg_[max_read_length];

        std::deque<char> received_data_;

        // buffered write data
        std::deque<char> write_msgs_;
        
         boost::thread * pThreadRead_;

        // etc.

        // timers

        boost::asio::deadline_timer timer1_;
        boost::asio::deadline_timer timer2_;

        // etc.

        // missing:
        // the whole async_write/async_read_some to serial line stuff

        void Process()
        {
                // MISSING CODE:
                // ... fill the send queue of the serial line cache and fire
                // async_write on serial port ...
                // Q: does this probaly interfere
                
                StartMemfun(0);
        }
                

        void StartMemFun1(int counter)
    {
                timer1_.cancel(); // Q: Is this necessary here?
                timer1_.expires_from_now(boost::posix_time::milliseconds(1000));
                timer1_.async_wait(bind(MemFun1, this, counter));
        }

        void MemFun1(int counter)
        {
                if (!(counter < 5)) Process();
                
                bool success;
                // ... process data from read cache
                if (success)
                        StartMemfun2(0);
                else
                        StartMemFun1(counter + 1); // this here works fine!
        }

        void StartMemFun2(int counter)
    {
                timer2_.cancel(); // Q: Is this necessary here?
                timer2_.expires_from_now(boost::posix_time::milliseconds(1000));
                timer2_.async_wait(bind(MemFun2, this, counter));
        }

        void MemFun2(int counter)
        {
                if (!(counter < 5)) Process();
                
                bool success;
                // ... process data from read cache
                if (success)
                        StartMemfun2(counter + 1);
                else
                        StartMemFun1(counter + 1);
        }
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net