All notes
Boos

Basics

Installation

cd tools/build
./bootstrap.sh
# Build in parallel with 4 cores.
b2 install --prefix=PREFIX -j4

What is b2, bjam?

boostDoc. The Boost.Build engine is derived from an earlier build tool called Perforce Jam. Originally, there were just minor changes, and the filename was bjam. Later on, with more and more changes, the similarity of names because a disservice to users, and as of Boost 1.47.0, the official name of the executable was changed to b2.

Thread

template <class F,class A1,class A2,...>
thread(F f,A1 a1,A2 a2,...);

Example: display progress

#include <boost/thread.hpp>
void printProgress(volatile int *progress, int total)
{
    int hit=0;
    do {
        if (*progress == total) hit++;
        system("sleep 1");
        cout<<"Progress: "<<*progress<<endl;
    } while (*progress < total && hit<=2);
}

int main()
{
	int progress = 0;
	boost::thread t1(boost::bind(run, &progress));
    boost::thread t2(boost::bind(printProgress, &progress, 100));
    t1.join();
    t2.join();
}

Thread_group

template<typename F>
thread* create_thread(F threadfunc);
Create a new boost::thread object as-if by new thread(threadfunc) and add it to the group. this->size() is increased by one, the new thread is running.

Multi-process boost

#include <boost/thread/thread.hpp>
thread_group group;
for(j=0; j<num_iterations; j++) {
	group.create_thread(boost::bind(&some_computation, this, result+j));
} 
group.join_all();

Bind

1.57.0 Ref.

#include <boost/bind.hpp>

Functions and function pointers

int f(int a, int b)
{
    return a + b;
}

int g(int a, int b, int c)
{
    return a + b + c;
}

int i = 5;
bind(f, i, _1); // The arguments that bind takes are copied and held internally by the returned function object.

bind(f, ref(i), _1); // Make the function object store a reference, rather than a copy
bind(f, cref(42), _1); // Constant reference.

Function objects

struct F
{
	int operator()(int a, int b) { return a - b; }
	bool operator()(long a, long b) { return a == b; }
};
F f;

int x = 104;
bind<int>(f, _1, _1)(x);		// f(x, x), i.e. zero
// A portable way
boost::bind(boost::type<int>(), f, _1, _1)(x);

struct F2
{
    int s;
    typedef void result_type;
    void operator()( int x ) { s += x; }
};

F2 f2 = { 0 };
int a[] = { 1, 2, 3 };

std::for_each( a, a+3, bind( ref(f2), _1 ) );

assert( f2.s == 6 );

pointers to members

class X
{
public:
	void f(args);
};

int main()
{
	X x;
	bind(&X::f, &x);
	// is equivalent to
	bind<R>(mem_fn(&X::f), &x);
	// where R is the return type of X::f (for member functions) or the type of the member (for data members.)
}

struct X
{
    bool f(int a);
};

X x;

shared_ptr<X> p(new X);

int i = 5;

bind(&X::f, ref(x), _1)(i);		// x.f(i)
bind(&X::f, &x, _1)(i);			//(&x)->f(i)
bind(&X::f, x, _1)(i);			// (internal copy of x).f(i)
bind(&X::f, p, _1)(i);			// (internal copy of p)->f(i)

Nested binds

bind(f, bind(g, _1))(x);               // f(g(x))

Example: Using bind with standard algorithms

class image;
class animation
{
public:
    void advance(int ms);
    bool inactive() const;
    void render(image & target) const;
};

std::vector<animation> anims;

template<class C, class P> void erase_if(C & c, P pred)
{
    c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
}

void update(int ms)
{
    std::for_each(anims.begin(), anims.end(), boost::bind(&animation::advance, _1, ms));
    erase_if(anims, boost::mem_fn(&animation::inactive));
}

void render(image & target)
{
    std::for_each(anims.begin(), anims.end(), boost::bind(&animation::render, _1, boost::ref(target)));
}

ASIO

"ASIO is an C++ I/O framework which is becoming part of the Boost library. It's like ACE updated for the STL era."