C++ - Boost Thread/Bind/Shared_ptr:断言错误
我有一个小问题无法解决。我正在制作一个小型服务器来将我的系统日志消息重定向到它。这是非常基本的,但我想知道我做错了什么,因为当我调用 join ()
时,我一直出现以下错误:
/boost/path/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = boost::thread]: Assertion `px != 0' failed.
代码将解释更多:
class SysLogServer
{
public:
typedef boost::shared_ptr<boost::thread> Ptr_thread;
bool Start ()
{
...
_thrd = Ptr_thread(new boost::thread (boost::bind(&SysLogServer::run, this)));
if (!_thrd.get ())
return ERROR ("Thread couldn't be instanciated.");
...
}
bool Stop ()
{
...
_thrd->join ();
...
}
private:
void run()
{
...
}
Ptr_thread _thrd;
};
非常感谢您的帮助。
PS:如果有任何改进可以更加“线程安全”,请告诉我,因为它真的让我感兴趣:)
编辑:
谢谢您的评论,我认为 shared_ptr
在那里确实没用,但是从 boost::enable_shared_from_this 继承该类可能很有用,以确保该类在线程结束之前不会被释放,而这种情况不应该发生。
Start()
当然是在 Stop()
之前调用的,我使用 state
属性执行简单的检查。 run()
方法只是接受连接。
class SysLogServer
{
public:
bool Start ()
{
...
_thrd = boost::thread(boost::bind(&SysLogServer::run, this)));
...
}
bool Stop ()
{
...
_thrd.join();
...
}
void run ()
{
std::cout << "Start running..." << std::endl; // never printed
// Create a socket
// Create a sockaddr_in
// Bind them together
while (!_serverStopped && !listen(sockfd, 5)) // on Stop(): _severStopped = true
{
// Get socket from accept
// Print the received data
// Close the socket given by accept
}
// close the first socket
}
boost::thread _thrd;
};
现在可以了。我之前使用指针几乎相同的解决方案,但没有成功,我的朋友 SIGSEGV :)
编辑 2:
它不适用于指针,因为我忘记检查服务器有的 Stop()
已开始。 Start()
方法因另一个原因而失败。
感谢您的有用建议
I am having a small issue that I cannot solve. I am making a small server to redirect my syslog messages to it. It is very basic, but I would like to know what I did wrong cause I keep have the following error when I call join ()
:
/boost/path/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = boost::thread]: Assertion `px != 0' failed.
The code will explain more:
class SysLogServer
{
public:
typedef boost::shared_ptr<boost::thread> Ptr_thread;
bool Start ()
{
...
_thrd = Ptr_thread(new boost::thread (boost::bind(&SysLogServer::run, this)));
if (!_thrd.get ())
return ERROR ("Thread couldn't be instanciated.");
...
}
bool Stop ()
{
...
_thrd->join ();
...
}
private:
void run()
{
...
}
Ptr_thread _thrd;
};
Thank you very much for your help.
PS: If there is any improvment to be more "thread safe", tell me cause it really interests me :)
Edit:
Thank you for your comments, I think that the shared_ptr
is indeed useless there but that it might me useful to inherit the class from boost::enable_shared_from_this
to ensure that the class is not freed before the end of the thread, which should not happen.
Start()
is of course called before Stop()
, I perform a simple check with a state
attribute. The run()
method is simply accepting connections.
class SysLogServer
{
public:
bool Start ()
{
...
_thrd = boost::thread(boost::bind(&SysLogServer::run, this)));
...
}
bool Stop ()
{
...
_thrd.join();
...
}
void run ()
{
std::cout << "Start running..." << std::endl; // never printed
// Create a socket
// Create a sockaddr_in
// Bind them together
while (!_serverStopped && !listen(sockfd, 5)) // on Stop(): _severStopped = true
{
// Get socket from accept
// Print the received data
// Close the socket given by accept
}
// close the first socket
}
boost::thread _thrd;
};
It works now. I used the almost same solution before with pointers, without any success and my friend SIGSEGV :)
Edit 2:
It didn't work with pointers cause I was forgetting to check in Stop()
that the server has been started. The Start()
method fails for another reason.
Thank you for your useful advices
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您在此处提供的代码中无法立即清楚断言的原因,但尽管如此,该代码可以得到显着改进。
您似乎正在使用
shared_ptr
,但似乎没有任何需要。Ptr_thread
可以更改为boost::thread
。这将导致代码更简单、更高效,并且更容易理解对象的生命周期。然后,代码可以更改为:
如果在调用
Start()
之前调用Stop()
,则此代码仍然不正确,这是对原始代码的唯一明显解释代码失败。The reason for the assertion is not immediately clear from the code that you have presented here, but nonetheless, this code could be improved significantly.
You appear to be using a
shared_ptr
, but there does not seem to be any need.Ptr_thread
could be changed to justboost::thread
. This would lead to simpler, more efficient code with easier to understand object lifetimes.The code could then be changed to:
This code is still incorrect if
Stop()
is called beforeStart()
has been called, which is the only obvious explanation for your original code failing.