为什么 boost 在这种情况下不创建线程(C2248)以及如何创建它?
所以我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了扩展 Ben 的答案,
boost::shared_ptr
是这里的常用机制:To expand on Ben's answer,
boost::shared_ptr<>
is the usual mechanism here:下次显示完整的错误消息。
您的问题是您的线程过程需要对套接字的引用,但套接字是局部变量。
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.