Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

What's New in Boost Threads?


Having somewhat languished unchanged for a couple of years, the Boost Thread library (boost.org) finally got some of the big changes it deserved with a major rewrite for the April 2008 release of Boost 1.35.0. Hot on the heels of 1.35.0 is the Boost 1.36.0 release in August 2008, which includes even more improvements to the Thread library. Certainly, a lot has changed since William Kempf wrote The Boost.Threads Library in 2002.

These releases have included some significant changes that either bring the library more in line with the proposals for the new C++0x thread library, or provide additional features as extensions. In this article, I present an overview of the new features and changes in the 1.35.0 and 1.36.0 releases from the user's point of view.

Launching Threads

As in prior releases of Boost, threads are started by constructing instances of boost::thread. However, there are a few notable changes to the boost::thread class.

One of the most significant changes is that boost::thread objects are now moveable. Taking advantage of the new rvalue-reference facility from C++0x where available (recent versions of GCC, for instance), and with a library-based move facility for other compilers, ownership of a specific thread of execution can be transferred between instances of boost::thread. This means that you can return objects of type boost::thread from a function, and store them in arrays or move-aware containers. This lets you encapsulate the thread creation process in a function without having to dynamically allocate the thread object.


boost::thread create_thread()
{
    void some_function();
    boost::thread t(some_function);
    return boost::move(t);
}
boost::thread threads[45];
threads[12]=boost::move(create_thread());

The second change is that you can now pass additional arguments to the boost::thread constructor, which are in turn passed on to the thread function:

void thread_func   (int i,double d,std::string s);
boost::thread t(thread_func, 42,3.141,"hello world");

This works exactly like boost::bind — in fact it uses boost::bind internally — so you can even pass member functions and a "this" pointer. It is important to note that the supplied arguments (including the thread function itself, if it's a function object) are copied into the newly created thread, so if you need to pass a reference you have to wrap it in boost::ref.

int i;
void thread_func(int& j);
boost::thread t   (thread_func,boost::ref(i));

For copyable types such as int, failure to use boost::ref will not be diagnosed, as the code just passes a reference to the copy of the original stored in the thread, silently giving unexpected results. Therefore, if your thread function takes its parameters by reference, you need to take extra care to ensure that boost::ref is used correctly.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.