Getting Started with Boost Threads in Visual Studio


This post aims to be an accessible introduction to getting set up with the Boost threads in Visual Studio environments for the first time.  Like with many technical subjects, there is a great deal of information out on the internet, that tells you a lot without actually showing you anything!

Following in the same vein as previous posts, these are instructions on how to get from A to B when installing Boost threads.  For a general introduction to getting started with Boost in Visual Studio environments see this post. has a comprehensive list of Boost thread tutorials also.

There is no in-depth discussion here on how to actually use the boost thread library, that’s for another time or another post or a different web page.  This is to merely help you get up and running, which for many can often seem the hardest part.

A simple boost::thread example

A nice introduction to boost thread programming exists over at Gavin Baker’s “” 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...

    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;  


    std::cout << "main: done" << std::endl;  

    return 0;

This code creates a boost thread object, passes it an example worker function and exits the thread when complete. The following sections describe the steps necessary to get started with boost::thread:

1. download and install Boost

Assuming that 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 (see this post), you are not quite finished yet.  It is likely that you will encounter a linker error similar to this when trying this simple example 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.  There exist a number of Boost libraries that you will need to build yourself and this library is one of them.

2. Obtain bjam

The next stage will be to build the bjam.exe program.  Probably the simplest way to do this 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:

Then 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:

3. Create the bjam.exe executable

When this is installed, go to the C:\Program Files\boost_1_46_1\tools\build\v2\engine\src directory and run build.bat from the command prompt.  Running the build.bat script will create the bjam.exe executable inside this directory:

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

4. Update the PATH environment variable

You now need to select the bjam.exe into in your PATH environment variables. In Windows 7 for example, right-click Computer, select Properties and Advanced System Settings:

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

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 (;):

5. Run the bjam executable

At the command prompt, go to the C:\Program Files\boost_1_46_1 directory, enter “bjam”, waiting for approximately 5-10 minutes while the program gets created:

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, or whatever the appropriate name for your boost thread library file is:

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

In Configuration properties, select C/C++ -> General -> Additional Include Directories…

… and use this to set the location of where you installed the Boost library folder:

7. Deal with compiler error messages, if any:

Try building your Visual Studio project now. You may get a compiler error message similar to the following:

error C2472: 'boost::detail::win32::interlocked_bit_test_and_set' cannot be generated in managed code: 'Found an intrinsic not supported in managed code'; compile with /clr to generate a mixed image
error C2472: 'boost::detail::basic_timed_mutex::unlock' cannot be generated in managed code: 'Found an intrinsic not supported in managed code'; compile with /clr to generate a mixed image

If this is the case then return to your project properties once more (via Properties → C/C++ → General) and edit the Common Language RunTime Support, and make sure it is set to (/clr) as highlighted:

Sometimes in Release Mode you will get error messages, or other different error messages.

error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in DataCollection.obj
error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in DataCollection.obj

The final part of the Visual Studio setup would be to ensure there are no mismatches between the Debug and Release mode property settings. In particular, in properties -> C/C++ -> Preprocessor make sure the preprocessor definitions are consistent:

Also, in properties -> C/C++ -> Code Generation -> Runtime Library be sure that this is set to the same value for Debug / Release modes:

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

Code Project® article available here.

More code examples on the next page: