我遇到一个我无法解决的小问题。我正在制作一个小型服务器来将我的系统日志消息重定向到它。这是非常基本的,但我想知道我做错了什么,因为我在拨打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()
方法因其他原因失败。
感谢您提供有用的建议
答案 0 :(得分:1)
断言的原因并未立即从您在此处提供的代码中清楚地看出,但是,此代码可能会得到显着改善。
您似乎使用shared_ptr
,但似乎没有任何需要。 Ptr_thread
可以更改为boost::thread
。这将导致更简单,更高效的代码,更容易理解对象的生命周期。
然后可以将代码更改为:
class SysLogServer
{
public:
bool Start ()
{
...
_thrd = boost::thread(boost::bind(&SysLogServer::run, this)));
...
}
bool Stop ()
{
...
_thrd.join();
...
}
private:
void run()
{
...
}
boost::thread _thrd;
};
如果在调用Stop()
之前调用Start()
,则此代码仍然不正确,这是原始代码失败的唯一明显解释。