1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#include <boost/enable_shared_from_this.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <list>
// cross-thread job queue. multiple threads can post jobs, one or many threads can execute jobs.
class job_queue
{
public:
typedef boost::function<void()> Job;
private:
boost::mutex mtx_;
boost::condition cnd_;
typedef std::list<Job> Jobs;
Jobs jobs_;
int stop_;
public:
// puts a job into the queue
void post(Job job)
{
boost::mutex::scoped_lock lock(mtx_);
jobs_.push_back(job);
cnd_.notify_one();
}
// pulls one job from the queue, returns false when stopped
bool pull(Job* job)
{
boost::mutex::scoped_lock lock(mtx_);
for(;;)
{ // handle spurious wake-ups
while(!stop_ && jobs_.empty())
cnd_.wait(lock);
if(!jobs_.empty() && 2 != stop_)
{
job->swap(jobs_.front()); // move efficiently, avoiding *job = jobs.front()
jobs_.pop_front();
return true;
}
else if(stop_)
{
return false;
}
}
}
// make pull() return false
void stop(bool cancel_jobs)
{
boost::mutex::scoped_lock lock(mtx_);
stop_ = 1 + cancel_jobs; // 1 - complete jobs, 2 - cancel jobs
cnd_.notify_all();
}
job_queue() : stop_() {}
~job_queue() { this->stop(true); }
};
//-----------------------------------------------------------------------
// create a thread which loops indefinitely, processing jobs off the queue
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
void loop(job_queue* queue)
{
std::cout << __PRETTY_FUNCTION__ << " thread id is: " << boost::this_thread::get_id() << std::endl;
job_queue::Job job;
// wait and execute jobs till stopped
while(queue->pull(&job))
job(); // execute the job
}
//-----------------------------------------------------------------------
boost::thread create_job_thread(job_queue* queue)
{
return boost::thread(boost::bind(loop, queue));
}
//-----------------------------------------------------------------------
// various job types (for different argument types)
// member function of the form 'T::fn()'
template<class T> struct cb0 { typedef void (T::*type)(); };
template <typename T>
class job_0
{
job_queue* queue;
typedef boost::function<void()> post_func;
post_func p_func;
public:
job_0(boost::shared_ptr<T> that, typename cb0<T>::type cb, job_queue* q) :
queue(q), p_func(boost::bind(cb, that)) {}
void post()
{
queue->post(p_func);
}
};
// member function of the form 'T::fn(shared_ptr<A1>)'
template<class T, typename A1> struct cb1 { typedef void (T::*type)(boost::shared_ptr<A1>); };
template <typename T, typename A0>
class job_1
{
job_queue* queue;
typedef boost::function<void(boost::shared_ptr<A0>)> post_func;
post_func p_func;
public:
job_1(boost::shared_ptr<T> that, typename cb1<T, A0>::type cb, job_queue* q) :
queue(q), p_func(boost::bind(cb, that, _1)) {}
void post(boost::shared_ptr<A0> data)
{
queue->post(boost::bind(p_func, data));
}
};
// member function of the form 'T::fn(shared_ptr<A1>, shared_ptr<A2>)'
template<class T, typename A1, typename A2> struct cb2 { typedef void (T::*type)(boost::shared_ptr<A1>, boost::shared_ptr<A2>); };
template <typename T, typename A0, typename A1>
class job_2
{
job_queue* queue;
typedef boost::function<void(boost::shared_ptr<A0>, boost::shared_ptr<A1>)> post_func;
post_func p_func;
public:
job_2(boost::shared_ptr<T> that, typename cb2<T, A0, A1>::type cb, job_queue* q) :
queue(q), p_func(boost::bind(cb, that, _1)) {}
void post(boost::shared_ptr<A0> data1, boost::shared_ptr<A0> data2)
{
queue->post(boost::bind(p_func, data1, data2));
}
};
//-----------------------------------------------------------------------
// example usage
struct data
{
int i;
double d;
float f;
std::string s;
data() : i(), d(), f(), s()
{
std::cout << "data ctor " << this << " on thread " << boost::this_thread::get_id() << std::endl;
}
void ptr()
{
std::cout << "data " << this << std::endl;
std::cout << "i=" << i << std::endl;
std::cout << "d=" << d << std::endl;
std::cout << "f=" << f << std::endl;
std::cout << "s=" << s << std::endl;
}
~data()
{
std::cout << "~data dtor " << this << " on thread " << boost::this_thread::get_id() << std::endl;
}
};
struct consumer
: boost::enable_shared_from_this<consumer>
{
typedef boost::shared_ptr<consumer> consumer_ptr;
typedef job_1<consumer, data> consumer_job;
boost::shared_ptr<consumer_job> job_;
consumer(job_queue &queue) :
job_(new consumer_job(consumer_ptr(this), &consumer::eat, &queue))
{ }
void eat(boost::shared_ptr<data> d)
{
std::cout << __PRETTY_FUNCTION__ << " " << this << " on thread " << boost::this_thread::get_id() << std::endl;
d->ptr();
if (d->i++ < 5)
{
d->d *= 2;
d->f /= 2;
job_->post(d);
}
}
};
int main()
{
std::cout << __PRETTY_FUNCTION__ << " thread id is: " << boost::this_thread::get_id() << std::endl;
job_queue queue;
// create a thread which processes data received from a queue
boost::thread another_thread = create_job_thread(&queue);
boost::shared_ptr<consumer> c(new consumer(queue));
// create data in this thread
boost::shared_ptr<data> d(new data);
d->i = 0;
d->d = 20.5;
d->f = 3.4;
d->s = "hello, world";
// post job to another thread for asynchronous processing
c->job_->post(d);
// stop the queue and let it complete all jobs
queue.stop(false);
another_thread.join();
return 0;
}
//-----------------------------------------------------------------------
|