You are not logged in.

#1 2011-11-12 19:20:06

yl3dy
Member
Registered: 2011-07-27
Posts: 19

[Solved][Boost] Condition_variable exception on thread termination

Hi folks,

I'm trying to implement an object that accepts some data and processes it in a separate thread (basically producer-consumer model). The object's thread should run until object destructor is called. To notify of new data I use condition_variable. The most important part of code (full here: http://pastebin.com/uEtU0KG1 , with irrelevant parts simplified) :

std::queue<std::string> msgs;
boost::mutex read_write_queue;
bool finish;
boost::condition_variable new_msgs;

void thread_func()
{
	std::string m;

	while(true)        // !!!
	{
		{ // lock begins
			boost::unique_lock<boost::mutex> lk(read_write_queue);
			while(msgs.empty())
			{
				new_msgs.wait(lk);
				if(finish)		// to be called on object destruction
				{
					lk.unlock();
					return;
				}
			}
			m = msgs.front();
			msgs.pop();
		} // end of lock
		std::cout << m << "\n";		// some processing of data
	}
}

The problem is following: on object destruction (at the end of main()) getting an error that condition_variable couldn't cleanly terminate:

a.out: /usr/include/boost/thread/pthread/condition_variable_fwd.hpp:46: boost::condition_variable::~condition_variable(): Assertion `!pthread_mutex_destroy(&internal_mutex)' failed.

When the line marked with 3 bangs (start of while cycle) is commented out, as if the object receives only one message, everything is ok. Have thought and googled about it a lot, but without any success.

I would be very grateful if someone would point at the fault (guess it's something simple).

Last edited by yl3dy (2011-11-13 09:21:48)

Offline

#2 2011-11-13 09:21:31

yl3dy
Member
Registered: 2011-07-27
Posts: 19

Re: [Solved][Boost] Condition_variable exception on thread termination

Well, I've finally figured out what was wrong. As I thought, the reason is quite simple. Destructor of an object didn't wait until thread will do all the termination things (unlocking condition variable, check for finish state and calling return). Adding a.join() at the end of destructor function will do the job.

What a stupid mistake.

Offline

Board footer

Powered by FluxBB