Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a std::thread with static linking causes segmentation fault

Tags:

c++

c++11

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.

like image 465
andre.hacker Avatar asked Jan 25 '12 11:01

andre.hacker


2 Answers

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.

like image 131
Denis Zaikin Avatar answered Sep 21 '22 15:09

Denis Zaikin


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.

like image 25
andre.hacker Avatar answered Sep 21 '22 15:09

andre.hacker