Click here to Skip to main content
Click here to Skip to main content

How to get started using Boost threads

By , 3 Dec 2011
Rate this:
Please Sign up or sign in to vote.

Introduction

This post aims to be an accessible step-by-step introduction to helping beginners get set up with the Boost threads in Visual Studio environments for the first time. Like with many technical subjects, there seems to be a great deal of information out there that tells you a lot but does not actually show you much!

This article contains no in-depth discussions on how to use Boost threads in all their different guises. That's for another time or place. What this article (hopefully) does is help you get up and running with Boost threads minus any compiler whines, which for me at least is often the hardest part.

A simple boost::thread example

A nice introduction to boost thread programming exists over at Gavin Baker’s “antonymn” page which I will take the liberty of reproducing here:

#include <iostream>  
#include <boost/thread.hpp>   
#include <boost/date_time.hpp>       
      
void workerFunc()  
{  
    boost::posix_time::seconds workTime(3);          
    std::cout << "Worker: running" << std::endl;    
      
    // Pretend to do something useful... 
    boost::this_thread::sleep(workTime);          
    std::cout << "Worker: finished" << std::endl;  
}    
      
int main(int argc, char* argv[])  
{  
    std::cout << "main: startup" << std::endl;          
    boost::thread workerThread(workerFunc);    
      
    std::cout << "main: waiting for thread" << std::endl;          
    workerThread.join();    
      
    std::cout << "main: done" << std::endl;          
    return 0;  
}

This example code creates a boost thread object, passes it an example worker function, and exits the thread when complete. This simple example I use as a means of validating the successful setup of the Boost thread library. The following sections describe the preliminary steps that will be necessary to run this simple example.

1. Download and install Boost

If you have not already done so, download and install the Boost libraries from here or from here. In your Visual Studio project, select Configuration Properties -> Additional Include Directories and enter the path to wherever you have installed the Boost root directory. At the time of obtaining the following screenshot, the version of Boost being used was 1.42.0 - just modify the directory path according to whichever version you are using:

threads9.png

Assuming you have downloaded, unzipped, and installed the Boost libraries in your Visual Studio environment, and told the Visual Studio project where the Boost libraries live, you are not quite finished yet. It is likely that you will encounter a linker error similar to this when compiling the above code for the first time:

LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-gd-1_46_1.lib'

It’s telling you that it does not know anything about this library file. In Boost, there exists a number of libraries that you are required to build yourself and this is one of them.

2. Obtain bjam

The next stage will be to build the bjam.exe program. I have found that the simplest way is to use the installers provided by BoostPro. In my example, this was the boostpro 1.46.1 installer. Download and run this program.

For me, it was the VS 2003 .NET and VS 2010 versions I was interested in:

threads1.png

During installation, select which of the libraries you wish to install – it does not have to be all of them, just the thread libraries if you wish:

threads2.png

3. Create the bjam.exe executable

When bjam has been installed, open up a command prompt and go to the src directory, for example:

C:\Program Files\boost_1_46_1\tools\build\v2\engine\src

Run build.bat from the command prompt. Notice that running this script will create the bjam.exe executable inside the bin.ntx86 subdirectory:

C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86

4. Update PATH environment variables

Now select bjam.exe into your PATH environment variables. In Windows 7 for example, right-click Computer, select Properties and Advanced System Settings:

threads3.png

Click on the Environment Variables… button and select the Edit button:

threads4.png

Include the directory:

C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86

as another environment variable, making sure each variable is separated by a colon (;):

threads5.png

5. Run the bjam executable

At the command prompt, go to the C:\Program Files\boost_1_46_1 directory and enter “bjam”, waiting approximately 5-15 minutes during which the program gets created:

threads6.png

6. Set Visual Studio project properties

In your Visual Studio project, select Configuration Properties -> Linker -> Input -> Additional Dependencies and enter libboost_thread-vc100-mt-gd-1_46_1.lib:

threads7.png

In your Visual Studio project, set the project configuration properties -> Linker -> General -> Additional Include Directories, telling it the location of the stage/lib folder:

threads8.png

And that’s it! This example Boost thread program should now compile and run, giving the following output:

main: startup
main: waiting for thread
Worker: running
Worker: finished
main: done

Other Boost-related articles and tutorials are available here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AndyUk06
Software Developer http://technical-recipes.com
United Kingdom United Kingdom
Software Engineer who has worked on projects in telecommunications, industrial robotics and image processing.
 
My home page is http://technical-recipes.com
Follow on   Twitter

Comments and Discussions

 
QuestionBoost installer Pinmembergeoyar14-Nov-11 9:35 
AnswerRe: Boost installer PinmemberAndyUk0615-Nov-11 3:19 
QuestionAn alternative to building Boost PinmemberNemanja Trifunovic7-Nov-11 6:35 
Not sure whether it still works with the current version of Boost, but back when I used it, I would simply add the missing Boost cpp files to my project, rather than building the lib and linking to it.

AnswerRe: An alternative to building Boost PinmemberAndyUk067-Nov-11 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 3 Dec 2011
Article Copyright 2011 by AndyUk06
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid