I have a few singleton classes with some extra functions which are run in separate thread. The structure looks like:
class Singleton
{
private:
boost::mutex mMutex;
std::vector<std::string> mMessages;
public:
void AddMessage(const std::string &msg)
{
mMutex.lock();
mMessages.push_back(msg);
mMutex.unlock();
}
void Sender()
{
while (true) {
mMutex.lock();
for (size_t i = 0; i < mMessages.size(); ++i)
{
// Do something with mMessages[i]
}
mMutex.unlock();
}
}
};
...
int main()
{
Singleton *handle;
handle = Singleton::instance();
boost::thread sender(boost::bind(&Singleton::Sender, handle));
... app cycle ...
}
Sometimes it fails with error:
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
what(): boost::lock_error
Aborted
What could it be and what’s the best why to find out the reason of assert?
May be that you did not create the object
Singleton! So the mutex is not created either.Try: