0

I have a constructor that creates two threads of a server (I am using the cpp-netlib library). The weird problem that I am getting is that even though I don't call servlet1.join() and servlet2.join() in the constructor, for some reason the constructor waits for the two threads to end. Even though these threads will never end. However, if I put the same code in main(), it will not wait for the two threads unless I call join(). Take a look at version A and B.

http_server* serv;

A-

Server()
{
    boost::network::utils::thread_pool thread_pool(2);
    Server handler();
    serv = new http_server(0.0.0, 800, handler, thread_pool);
    boost::thread servlet1(boost::bind(&http_server::run, serv));
    boost::thread servlet2(boost::bind(&http_server::run, serv));
    serv->run();
    std::cout << "This never prints" << std::endl;
}
~Server()
{
    serv->stop(); //this kills all threads and stops server gracefully
    delete serv;
}

main:

int main()
{
    std::cout << "hi" << std::endl; //this prints
    Server* test = new Server();
    std::cout << "hi" << std::endl; //this never prints
    delete test;
}

B-

int main()
{
    boost::network::utils::thread_pool thread_pool(2);
    Server handler();
    serv = new http_server(0.0.0, 800, handler, thread_pool);
    boost::thread servlet1(boost::bind(&http_server::run, serv));
    boost::thread servlet2(boost::bind(&http_server::run, serv));
    serv->run();
    std::cout << "This always prints" << std::endl;
}
marcoo
  • 821
  • 1
  • 9
  • 25

1 Answers1

2

You have an infinite loop because you are instantiating a Server in the Server() constructor

in A-

Server()
{
    boost::network::utils::thread_pool thread_pool(2);

    ***--> Server handler(); <--***

    serv = new http_server(0.0.0, 800, handler, thread_pool);
    boost::thread servlet1(boost::bind(&http_server::run, serv));
    boost::thread servlet2(boost::bind(&http_server::run, serv));
    serv->run();
    std::cout << "This never prints" << std::endl;
}
David Hope
  • 2,216
  • 16
  • 32
  • Ha. I failed to even notice that. – Tebc Feb 21 '13 at 19:58
  • I'm surprised that you didn't get an out of memory error or the like. On the other hand, if you are running 64-bit and have lots of disk space...it might take a while, thrashing and all. – David Hope Feb 21 '13 at 20:44
  • 1
    `Server handler();` declare a function, where as `Server handler;` declares an object. That line alone will not create a synchronous infinite loop. It is hard to tell where the issue is with nonconforming code, but if `handler()` function is being invoked within the calling thread, then an infinite loop would exists. – Tanner Sansbury Feb 21 '13 at 20:52
  • That line indeed declares a function. That and the 0.0.0 makes me wonder how close to the actual code the shown code is. The only thing I saw there was the local thread pool, which doesn't make much sense. I think there's simply not enough info provided to analyse the issue. – Ulrich Eckhardt Feb 22 '13 at 22:23
  • we may never know what the actual code looks like unless the OP comes back and tells us, but he indicated that my answer solved his problem, so until told otherwise, I'm going to assume that his line `Server handler();` was actually `Server handler`. It seems unlikely that the http_server constructor would have accepted an uninitialized function declaration as a parameter. – David Hope Feb 22 '13 at 23:54