启动具有静态链接的std ::线程会导致分段错误 [英] Starting a std::thread with static linking causes segmentation fault

查看:113
本文介绍了启动具有静态链接的std ::线程会导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要学习c ++ 11(和boost),我使用boost asio和c ++ 11(用于线程和lambdas)编写一个简单的http服务器。



我想测试新的c ++ 11 lambdas和std ::线程,所以我试图在std ::线程中启动 io_service.run()一个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_ :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服务器(2);
server.Start();
}

另外有时它运行cout里面lambda,有时不(尽管endl应该flush)。在任何情况下,它打印正确的地址 io_service _ 。但是,当我用 boost :: thread (没有其他更改!)替换 std :: thread 正在工作。



如果任何人有一个想法的原因(可能是asio,std :: thread或std :: lambda)。 / p>

其他信息:



根据另一个这 时,访问lambda内的成员io_service_的成员函数7895879 / using-member-variable-in-lambda-capture-list-inside-a-member- code>,因为我这样做。



我在Ubuntu上运行gcc 4.6.1并提升1.46。 G ++参数:

  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 


$ b b

更新:



删除-static已解决问题。我发现,这个问题与boost或lambdas无关,并可以重现每当构建静态和使用 std :: thread 。由于任何原因,此组合不工作。
我认为这
帖子描述的几乎相同,但我不真正理解细节和错误消息是不同的。



所以我不知道为什么 std :: thread 似乎没有一起工作。有没有理由为什么静态链接不允许这里?我更新了问题标题,并移除了boost标记。

解决方案

我发现它与boost或lambdas没有任何关系,但是与静态链接和 std :: thread ,似乎没有一起工作的任何未知的原因请参阅可能相关的此帖子)。



我想问题为什么他们不一起工作是 - 虽然有趣 - 现在范围之外,我很高兴与答案,所以这可以标记为已回答。


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.

解决方案

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.

这篇关于启动具有静态链接的std ::线程会导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆