Boost asio/bind

Hi

Ive made some modifications to the example code provided on the boost site just to get a better understanding of how everything works.

What i cant work out though is why the output is how it is. Why isnt the destructor for p called after it reaches 2 and why does the output from q wait until p has reached the end before displaying.

If anyone can just run me through everything the program does from start to finish that would be a huge help.

If not any help would be appreciated

Thanks


PROGRAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:

    printer(boost::asio::io_service& io, int num):
        timer(io, boost::posix_time::seconds(0)),count(0),count2(1)
        {
            timer.async_wait(boost::bind(&printer::print, this));
            maximum = num;
        }

   ~printer()
    {std::cout << "Final count is " << count << "\n";}

    void print()
    {
        if (count < maximum)
        {
            std::cout << count << "\n";
            ++count;

            timer.expires_at(timer.expires_at() + boost::posix_time::seconds(1));
            timer.async_wait(boost::bind(&printer::print, this));
        }
    }

private:
    boost::asio::deadline_timer timer;
    int count;
    int count2;
    int maximum;
};


int main()
{
  boost::asio::io_service io;
  boost::asio::io_service is;
  printer p(io,2);
  printer q(is,10);
  io.run();
  is.run();

  return 0;
}


Output


0
1
0
1
2
3
4
5
6
7
8
9
Final count is 10
Final count is 2

Process returned 0 (0x0)   execution time : 10.037 s
Press any key to continue.
jidder wrote:
Why isnt the destructor for p called after it reaches 2
Why would the destructor for p be called when it reaches 2? When do you think destructors get called??
When the class is destroyed ?
I realise that was a pretty stupid question now.
Any help on what everything does though.

That comment wasnt very constructive.
That comment wasnt very constructive.

Yet it challenged your incorrect assumption about when classes are destroyed. I'd say it was constructive enough.

Your question on the other hand is rather vague, and you should expect answers that reflect that. A good question will enumerate the beliefs you have about the code, and the ways the output differs from what you expected. A good question will make it easier on people to answer.

Do you believe the output should be interleaved? Why do you believe that? Have you read the documentation for the objects you're using?

http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_service/run/overload1.html

Boost Documentation wrote:
The run() function blocks until all work has finished and there are no more handlers to be dispatched, or until the io_service has been stopped.

Last edited on
Topic archived. No new replies allowed.