Hi all

I'm designing a multithreaded application which has long running threads. I need to check some condititons in the main thread, interrupt the threads and end the program if the conditions are true. But i'm not sure if this is the correct/safest way. Here's my pseudo:

main()
{
	boost::thread t1(f1);
	boost::thread t2(f2);

	//while (1)
	//{
		//check for some conditions
		//if (condition1)
		//{
		//	t1.interrupt();
		//	t2.interrupt();
		//}
	//}
	t1.join();
	t2.join();
}

if the main thread runs the while loop, the threads t1,t2 will not join never. So is this safe? or what can i do else?

thanks in advance.

Recommended Answers

All 4 Replies

Safest would be to have some shared variables between teh monitoring thread (one running main) and monitored threads (t1, t2). When the condition is met, monitoring thread sets the variable to indicate that monitored threads should exit now.
This would give an option to t1/2 to gracefully shutdown.
Also I suggest to use timed_join() instead of join().

why don't you use pthreads(POSIX threads), in that way you can use synchronization with mutexes and condition variables.

///
/// below is pseudocode/pattern on a possible way to
/// start a pool of thread and end them gracefully
/// need thread and conditon variable implementation
///

#include <iostream>
#include <vector>
///
/// replace with your thread class
///
class Thread
{
public:
	virtual ~Thread(){;}
protected:	
	virtual void run() = 0;
};

///
/// worker thread to do actual work
///
class Worker : public Thread
{
public:
	Worker(size_t who_)
	:_isAlive(false)
	,_who(who_)
	{}
	 /// start thread
	 void start()
	 {
	 	_isAlive = true;
		// start your thread;
	 }
	 /// main call worker to end
	 /// and waits for worker to end
	 void end()
	 {
		_isAlive = false;
		// _event.wait();
	 }
private:	 
	 // thread enters here
	 virtual void run()
	 {
	 	std::cout << "thread who=[" << _who << "] is starting" << std::endl;;

		while(_isAlive)
		{
			// do work
		}

	 	std::cout << "thread who=[" << _who << "] is ending" << std::endl;;

		// signal thread is done
		//_event.signal();
	 }
private:
	size_t _who;
	bool _isAlive;
	// condition _event;
};

int main()
{
	std::cout << "starting main thread" << std::endl;

	const size_t MAX_POOL = 8;
	std::vector<Worker *> pool;
	for(size_t i=0; i < MAX_POOL; i++ )
	{
		Worker *w = new Worker(i);
		pool.push_back(w);
		w->start();
	}

	// wait for condition to end threads


	// tell each worker to end
	for(size_t i=0; i < MAX_POOL; i++)
	{
		pool[i]->end();
	}

	std::cout << "main thread is done" << std::endl;
}
///
/// below is pseudocode/pattern on a possible way to
/// start a pool of thread and end them gracefully
/// need thread and conditon variable implementation
///

#include <iostream>
#include <vector>
///
/// replace with your thread class
///
class Thread
{
public:
	virtual ~Thread(){;}
protected:	
	virtual void run() = 0;
};

///
/// worker thread to do actual work
///
class Worker : public Thread
{
public:
	Worker(size_t who_)
	:_isAlive(false)
	,_who(who_)
	{}
	 /// start thread
	 void start()
	 {
	 	_isAlive = true;
		// start your thread;
	 }
	 /// main call worker to end
	 /// and waits for worker to end
	 void end()
	 {
		_isAlive = false;
		// _event.wait();
	 }
private:	 
	 // thread enters here
	 virtual void run()
	 {
	 	std::cout << "thread who=[" << _who << "] is starting" << std::endl;;

		while(_isAlive)
		{
			// do work
		}

	 	std::cout << "thread who=[" << _who << "] is ending" << std::endl;;

		// signal thread is done
		//_event.signal();
	 }
private:
	size_t _who;
	bool _isAlive;
	// condition _event;
};

int main()
{
	std::cout << "starting main thread" << std::endl;

	const size_t MAX_POOL = 8;
	std::vector<Worker *> pool;
	for(size_t i=0; i < MAX_POOL; i++ )
	{
		Worker *w = new Worker(i);
		pool.push_back(w);
		w->start();
	}

	// wait for condition to end threads


	// tell each worker to end
	for(size_t i=0; i < MAX_POOL; i++)
	{
		pool[i]->end();
	}

	std::cout << "main thread is done" << std::endl;
}

In case you didn't notice, he's using boost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.