O'Reilly logo

Boost C++ Application Development Cookbook by Antony Polukhin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reference counting of pointers to classes used across methods

Imagine that you have some dynamically allocated structure containing data, and you want to process it in different execution threads. The code to do this is as follows:

#include <boost/thread.hpp> #include <boost/bind.hpp> void process1(const foo_class* p); void process2(const foo_class* p); void process3(const foo_class* p); void foo1() { while (foo_class* p = get_data()) // C way { // There will be too many threads soon, see // recipe 'Executing different tasks in parallel' // for a good way to avoid uncontrolled growth of threads boost::thread(boost::bind(&process1, p)) .detach(); boost::thread(boost::bind(&process2, p)) .detach(); boost::thread(boost::bind(&process3, p)) .detach(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required