小编and*_*ker的帖子

使用静态链接启动std :: thread会导致分段错误

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

我想测试新的c ++ 11 lambdas和std :: thread,所以我尝试io_service.run()在带有lambda的std :: thread中启动这样的:

#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_;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

11
推荐指数
2
解决办法
2932
查看次数

标签 统计

c++ ×1

c++11 ×1