为什么 boost 在这种情况下不创建线程(C2248)以及如何创建它?

发布于 2024-11-01 08:03:53 字数 1456 浏览 1 评论 0原文

所以我有这样的代码:

using namespace boost::asio::ip;
using namespace std;

void request_response_loop(boost::asio::ip::tcp::socket& socket)
{
    http_request request(socket);
    http_response response;
    response.body = "<head></head><body><h1>It Rocks!</h1></body>";
    response.send(socket);
    socket.close();
    cout << "connection resolved." << endl;
}


void acceptor_loop(){
    boost::asio::io_service io_service;
    int m_nPort = 12345;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
    cout << "Waiting for connection..." << endl;
    while(true)
    {
        try
        {
            tcp::socket socket(io_service);
            acceptor.accept(socket);
            cout << "connection accepted." << endl;
            boost::thread workerThread2(request_response_loop, socket); // here it does not compile because of C2248
        }
        catch(exception &e)
        {
            cerr << e.what() << endl; //"The parameter is incorrect" exception
        }
    }
}
int main()
{ 
    boost::thread workerThread(acceptor_loop);
    cin.get();
}

它给了我错误:

Error   1   error C2248: boost::noncopyable_::noncopyable::noncopyable: "boost::noncopyable_::noncopyable"  boost\asio\basic_io_object.hpp  93  

如何能够在我用于套接字接受的另一个线程中运行我的 request_response_loop 函数?

So I have code like:

using namespace boost::asio::ip;
using namespace std;

void request_response_loop(boost::asio::ip::tcp::socket& socket)
{
    http_request request(socket);
    http_response response;
    response.body = "<head></head><body><h1>It Rocks!</h1></body>";
    response.send(socket);
    socket.close();
    cout << "connection resolved." << endl;
}


void acceptor_loop(){
    boost::asio::io_service io_service;
    int m_nPort = 12345;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
    cout << "Waiting for connection..." << endl;
    while(true)
    {
        try
        {
            tcp::socket socket(io_service);
            acceptor.accept(socket);
            cout << "connection accepted." << endl;
            boost::thread workerThread2(request_response_loop, socket); // here it does not compile because of C2248
        }
        catch(exception &e)
        {
            cerr << e.what() << endl; //"The parameter is incorrect" exception
        }
    }
}
int main()
{ 
    boost::thread workerThread(acceptor_loop);
    cin.get();
}

It gives me error:

Error   1   error C2248: boost::noncopyable_::noncopyable::noncopyable: "boost::noncopyable_::noncopyable"  boost\asio\basic_io_object.hpp  93  

How to make it possible to run my request_response_loop function in another thread that one I use for socket accepting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-11-08 08:03:53

为了扩展 Ben 的答案,boost::shared_ptr 是这里的常用机制:

void request_response_loop(boost::shared_ptr<tcp::socket> socket)
{
    http_request request(*socket);
    http_response response;
    response.body = "<head></head><body><h1>It Rocks!</h1></body>";
    response.send(*socket);
    socket->close();
    cout << "connection resolved." << endl;
}

void acceptor_loop()
{
    boost::asio::io_service io_service;
    int m_nPort = 12345;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
    cout << "Waiting for connection..." << endl;
    while(true)
    {
        try
        {
            boost::shared_ptr<tcp::socket> socket =
                boost::make_shared<tcp::socket>(boost::ref(io_service));
            acceptor.accept(*socket);
            cout << "connection accepted." << endl;
            boost::thread workerThread2(request_response_loop, socket);
        }
        catch(exception &e)
        {
            cerr << e.what() << endl;
        }
    }
}

To expand on Ben's answer, boost::shared_ptr<> is the usual mechanism here:

void request_response_loop(boost::shared_ptr<tcp::socket> socket)
{
    http_request request(*socket);
    http_response response;
    response.body = "<head></head><body><h1>It Rocks!</h1></body>";
    response.send(*socket);
    socket->close();
    cout << "connection resolved." << endl;
}

void acceptor_loop()
{
    boost::asio::io_service io_service;
    int m_nPort = 12345;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
    cout << "Waiting for connection..." << endl;
    while(true)
    {
        try
        {
            boost::shared_ptr<tcp::socket> socket =
                boost::make_shared<tcp::socket>(boost::ref(io_service));
            acceptor.accept(*socket);
            cout << "connection accepted." << endl;
            boost::thread workerThread2(request_response_loop, socket);
        }
        catch(exception &e)
        {
            cerr << e.what() << endl;
        }
    }
}
软的没边 2024-11-08 08:03:53

下次显示完整的错误消息。

您的问题是您的线程过程需要对套接字的引用,但套接字是局部变量。 acceptor_loop 函数不会等待,因此一旦超出范围,它就会销毁套接字。

对于线程之间传递的变量,您需要使用动态或静态生命周期,而不是自动生命周期。

Next time show the entire error message.

Your problem is that your thread procedure wants a reference to the socket, but the socket is a local variable. The acceptor_loop function doesn't wait, so it will destroy the socket as soon as it goes out of scope.

You need to use dynamic or static lifetime, never automatic, for variables passed between threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文