Now that we know how to start threads of execution, we want to have access to some common resources from different threads:
#include <cassert>
#include <cstddef>
#include <iostream>
// In previous recipe we included
// <boost/thread.hpp>, which includes all
// the classes of Boost.Thread.
// Following header includes only boost::thread.
#include <boost/thread/thread.hpp>
int shared_i = 0;
void do_inc() {
for (std::size_t i = 0; i < 30000; ++i) {
const int i_snapshot = ++shared_i;
// Do some work with i_snapshot.
// ...
}
}
void do_dec() {
for (std::size_t i = 0; i < 30000; ++i) {
const int i_snapshot = --shared_i;
// Do some work with i_snapshot.
// ...
}
}
void run() {
boost::thread t1(&do_inc);
boost::thread t2(&do_dec);
t1.join();
t2.join...