Using boost::signal2 for signaling between threads...

Started by
1 comment, last by Toolmaker 14 years, 5 months ago
I'm currently working on a little system that requires that I use inter-thread communication (ITC) to achieve the exchange of events between threads. I decided to give boost a go and came down to using boost::thread and boost::signal2. I decided to write myself a little testing app, consisting of 2 classes, one that counts up, and one that downs count. One of them runs faster than the other and when it's done, it will signal the other, which will cry out in shame and then stop. However, I can't get my example to compile. Could anyone shed some light on what I am doing wrong? I exclude the Countdown class, because all it does it count differently. The code posted below is compile ready, and the current error I get is: error C2228: left of '.join' must have class/struct/union No idea what I did wrong with the construction of my thread.

#include <boost/thread.hpp>
#include <boost/signals2.hpp>

const int max = 20;
class Base;

typedef boost::signals2::signal<void (Base *)> Signal;

class Base
{
public:
	Base(Signal &sig) : m_sig(sig)
	{
		m_sig.connect(this);
	}

	// Signal function
	void operator() (Base *pCaller)
	{
		if (pCaller != this)
			std::cout << "Oh shit! We've been beaten!" << std::endl;
	}

protected:
	Signal &m_sig;
};


class Countup : Base
{
public:
	Countup(Signal &sig) : Base(sig)
	{ }

	void operator() ()
	{
		int x = 0;

		while (x < max)
		{
			x++;
			std::cout << "Counting up: " << x << std::endl;
			boost::this_thread::sleep(boost::posix_time::milliseconds(750));
		}
		
		m_sig(this);
	}
};

class Countdown : Base
{
public:
	Countdown(Signal &sig) : Base(sig)
	{
	}

	void operator() ()
	{
		int x = max;
		while (x > 0)
		{
			x--;
			std::cout << "Counting down: " << x << std::endl;
			boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
		}

		m_sig(this);
	}
};

int main()
{
	Signal sig;
	boost::thread t1(Countup(sig)), t2(Countdown(sig));

	t1.join();
	t2.join();

	return 0;
}


The idea is that in the next step, I implement a system in our application that uses threads to communicate with a managing thread to inform that thread of the current progress and when it's done(That's what the signal is for). Toolmaker

Advertisement
Ugh, don't go into this mess. There are way too many issues.

Use asio's io_service with either post() or dispatch(), whichever is non-blocking. That allows you to safely schedule and dispatch callbacks between threads and takes care of synchronization and many other issues.
Quote:Original post by Antheus
Ugh, don't go into this mess. There are way too many issues.

Use asio's io_service with either post() or dispatch(), whichever is non-blocking. That allows you to safely schedule and dispatch callbacks between threads and takes care of synchronization and many other issues.


Even better then. I was actually looking into a similar thing, but I'm not very familiar with boost. I'll change to asio's io_service. thx <3

This topic is closed to new replies.

Advertisement