16

To learn c++11 (and boost) I am writing a simple http-server using boost asio and c++11 (for threading and lambdas).

I want to test the new c++11 lambdas and std::thread so I tried to start the io_service.run() like this in a std::thread with a lambda:

#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
using std::cout;
using std::endl;
using boost::asio::ip::tcp;

class HttpServer
{
public:
    HttpServer(std::size_t thread_pool_size)
    : io_service_(),
    endpoint_(boost::asio::ip::tcp::v4(), 8000),
    acceptor_(io_service_, endpoint_)
    { }

    void Start() {
        acceptor_.listen();
        cout << "Adr before " << &io_service_ << endl;
        std::thread io_thread([this](){
            cout << "Adr inside " << &io_service_ << endl;
            io_service_.run();
        });
        io_thread.join();
    }

private:
    boost::asio::io_service io_service_;
    tcp::endpoint endpoint_;
    tcp::acceptor acceptor_;
};

int main() {
    HttpServer server(2);
    server.Start();
}

This terminates with segmentation fault. Additionally sometimes it is running the cout inside the lambda, sometimes not (although endl should flush). In any case, it prints the correct address of io_service_. However, when I replace the std::thread with a boost::thread (no other change!), everything is working fine.

I would appreciate it if anyone has an idea where the problem is caused (could be asio, std::thread or std::lambda).

Additional information:

According to another post accessing the member io_service_ within a lambda is fine when capturing this, as I do it.

I am running gcc 4.6.1 on Ubuntu and boost 1.46. G++ parameters:

g++ -std=c++0x -static -I/home/andre/DEV/boost_1_48_0/include/ -L/home/andre/DEV/boost_1_48_0/lib/ -o webserver main.cpp -lboost_system -lboost_thread -lpthread

Update:

Removing -static fixed the problem. I found out that the problem has nothing to do with boost or lambdas and can be reproduced whenever building static and using std::thread. For any reason this combination does not work. I think this post describes almost the same, however I don't really understand the details and the error message is different.

So I wonder why std::thread and static linking don't seem to work together. Is there a reason why static linking is not allowed here? I updated the question title and removed the boost tag.

Community
  • 1
  • 1
andre.hacker
  • 303
  • 2
  • 9
  • I know the code above does nothing at this time. I only left what is relevant for the described problem... – andre.hacker Jan 25 '12 at 12:03
  • Have you tried running it in a debugger to see where the segmentation fault is? – Some programmer dude Jan 25 '12 at 12:05
  • 2
    @JoachimPileborg sorry, missed that. The segfault was sometimes in the first line of the lambda (cout << ...) and other times in the second line (io_service_.run();). Finally I found out that it has nothing to do with boost or lambdas, but with static linking and std::thread. Removing -static solved the problem. I found a similar post describing such [problems](http://stackoverflow.com/questions/7090623/c0x-thread-static-linking-problem). – andre.hacker Jan 25 '12 at 17:15
  • 2
    @andre.hacker post that comment as an answer – Sam Miller Jan 25 '12 at 20:02

2 Answers2

18

Link your app with -Wl,--whole-archive -lpthread -Wl,--no-whole-archive More here https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590 It works for me.

Denis Zaikin
  • 569
  • 4
  • 10
  • 4
    its a crying shame that even in gcc 6.2 the result of just linking with `-static -pthread` will result in broken executables (i.e. runtime-errors) and no linker errors or even warnings. And I had to spend several hours debugging only to find the issue is known since gcc 4.7 and was resolved as `RESOLVED INVALID`. Your answer finally pointed me to what I was doing wrong. – Julian Nov 13 '16 at 19:38
  • 1
    You are a life saver. Thanks! – Loves Probability Dec 10 '16 at 05:34
  • 3
    For anyone coming here and wondering, yes this is still necessary in 2020 with GCC 10. – Jean-Michaël Celerier Sep 08 '20 at 08:51
9

Removing -static solved the problem. I found out that it has nothing to do with boost or lambdas, but with static linking and std::thread, which don't seem to work together for any unknown reason (see also this post that might be related).

I guess the question why they don't work together is - though interesting - out of scope for now, and I am glad with the answer, so this can be marked as answered.

Community
  • 1
  • 1
andre.hacker
  • 303
  • 2
  • 9