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 SysL开发者_C百科ogServer
{
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
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 just boost::thread
. This would lead to simpler, more efficient code with easier to understand object lifetimes.
The code could then be changed to:
class SysLogServer
{
public:
bool Start ()
{
...
_thrd = boost::thread(boost::bind(&SysLogServer::run, this)));
...
}
bool Stop ()
{
...
_thrd.join();
...
}
private:
void run()
{
...
}
boost::thread _thrd;
};
This code is still incorrect if Stop()
is called before Start()
has been called, which is the only obvious explanation for your original code failing.
精彩评论