Boost – Thread pool

Boost Library

  • Boost is a collection of high-quality C++ libraries and works on almost any modern operating system, including UNIX and Windows variants

Compiling boost library

bjam toolset=msvc-6.0 link=static link=dynamic threading=multi

 

Features

Policy-based thread pool implementation

  • Scheduling policies: fifo, lifo and priority
  • Size policies: static_size
  • Size policy controller: empty_controller, resize_controller
  • Shutdown policies: wait_for_all_tasks, wait_for_active_tasks, immediately

Smooth integration into STL and boost

 

Basic constructs

Create thread pool

boost::threadpool::pool threadPool;

Schedule job

threadPool.schedule(&workerfunction);

Wait for completion

threadPool.wait();

Boost bind

m_tp.schedule(boost::bind(boost::mem_fn(&CDataProcessor::ProcessBuffer), this, iBuffPos));

 

Illustration – 1

#include "threadpool.hpp“
using namespace boost::threadpool;
// Some example tasks
void first_task()
{
  ...
}
void second_task()
{	
  ...
}
void execute_with_threadpool()
{
  // Create a thread pool.
  pool tp(2);
  
  // Add some tasks to the pool.
  tp.schedule(&first_task);
  tp.schedule(&second_task);
  // Leave this function and wait until all tasks are finished.
}

 

Illustration – 2

scoped_pool<prio_pool, 0> tp;

// Add some tasks to the pool.
tp += prio_task_func(5, &normal_task);
tp += prio_task_func(100, &important_task);
tp += prio_task_func(7, &normal_task);

// Add the some threads to the pool. This will start the execution of the tasks.
tp->resize(2);

// The tasks are processed according to their priority: important_task(100), nonrelevant_task(7), nonrelevant_task(5).

tp->wait();

 

Thread synchronization

Mutex

  • Protection against data races and allows thread-safe synchronization of data between threads.
  • Lockable, TimedLockable, SharedLockable and UpgradeLockable

Condition variables

  • Provide a mechanism for one thread to wait for notification from another thread that a particular condition has become true.

One-Time Initialization

  • Mechanism for ensuring that an initialization routine is run exactly once without data races or deadlocks

Barriers

  • A barrier is configured for a particular number of threads (n), and as threads reach the barrier they must wait until all n threads have arrived

 

Thread synchronization

One-Time Initialization

#include <boost/threadpool.hpp>
#include <boost/thread/once.hpp>

int value=0;
boost::once_flag once = BOOST_ONCE_INIT;

void init()
{
  ++value;
}
void thread_proc()
{
  boost::call_once(&init, once);
}
int main(int argc, char* argv[])
{
  boost::threadpool::pool tp(2);
  
  tp.schedule(&thread_proc);
  tp.schedule(&thread_proc);
  
  tp.wait();
}

 

#include <boost/thread/thread.hpp>
#include <boost/thread/once.hpp>
#include <cassert>

int value=0;
boost::once_flag once = BOOST_ONCE_INIT;

void init()
{
  ++value;
}

void thread_proc()
{
  boost::call_once(&init, once);
}

int main(int argc, char* argv[])
{
  boost::thread_group threads;
  for (int i=0; i<5; ++i)
  threads.create_thread(&thread_proc);
  threads.join_all();
  assert(value == 1);
}

 

😎

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *