I implemented a fairly basic system for asynchronous calls (without return value, so no futures are needed. basically just injecting code into another thread). The purpose is to be able to call a function from one thread and have it executed in another. The system works (it's very basic), but there might be a better way I don't know about (which is why i'm posting here)
AsyncCallable.hpp
#ifndef ASYNC_CALLABLE_HPP
#define ASYNC_CALLABLE_HPP
#include <vector>
#include <boost/function.hpp>
#include <boost/thread/mutex.hpp>
class AsyncCallable
{
protected:
void appendCall(boost::function<void (void)> call);
void executeCalls();
private:
typedef std::vector<boost::function<void (void)> > call_vec;
call_vec callsFront;
call_vec callsBack;
boost::mutex callMutex;
};
#endif
AsyncCallable.cpp
#include "AsyncCallable.hpp"
void AsyncCallable::appendCall(boost::function<void (void)> call)
{
callMutex.lock();
callsBack.push_back(call);
callMutex.unlock();
}
void AsyncCallable::executeCalls()
{
callMutex.lock();
callsFront.swap(callsBack);
callMutex.unlock();
for(call_vec::iterator it = callsFront.begin(); it != callsFront.end(); ++it)
(*it)();
callsFront.resize(0);
}
A simple task using AsyncCallable:
class TestCallable : private AsyncCallable
{
public:
TestCallable() : running(true) {}
void run()
{
std::cout << "Howdy!\n";
while(running)
{
executeCalls();
std::cout << "Bla\n";
Sleep(123);
}
std::cout << "Bye!\n";
}
void saySomething(const std::string& sth)
{
appendCall(boost::bind(&TestCallable::_saySomething,this,sth));
}
void stop()
{
appendCall(boost::bind(&TestCallable::_stop,this));
}
private:
bool running;
void _saySomething(const std::string& sth)
{
std::cout << sth << "\n";
}
void _stop()
{
running = false;
}
};