• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Toolmaker  Members

Posted 16 November 2009 - 02:33 AM

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

### #2Antheus  Members

Posted 16 November 2009 - 02:40 AM

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.

### #3Toolmaker  Members

Posted 16 November 2009 - 02:42 AM

Quote:
 Original post by AntheusUgh, 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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.