Boost thread usage

Source: Internet
Author: User
ArticleDirectory
    • How to get started using boost threads
How to get started using boost threads

ByAndyuk06| 7 Nov 2011 | unedited contribution

C ++ threadingboost

A non-Nonsense guide to setting up boost threads in Visual Studio Environments

Sored links

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 your technical subjects, there seems to be a great deal of information out there that tells you a but lot 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 whinges, 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 "policymn" page which I will take the liberty of reproducing here:

Collapse | copy code

 # include 
  
    # include 
   
     # include 
    
      void workerfunc () {boost: posix_time: seconds worktime (3); STD: cout <"worker: running "
     
    
   
  

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

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 exist 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 buildBjam.exeProgram. I have found that the simplest way is to use the installersprovided 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:

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:

3. Create 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_000046_1 \ tools \ build \ V2 \ engine \ SRC

RunBuild. batFrom the command prompt. Notice that running this script will createBjam.exeExecutable insideBin. ntx86Subdirectory:

C: \ Program Files \ boost_000046_1 \ tools \ build \ V2 \ engine \ SRC \ bin. ntx86

4. Update PATH environment variables

Now selectBjam.exeInto 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_000046_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 toC: \ Program Files \ boost_000046_1Directory and enter "bjam", waiting approximately 5-15 minutes during which the program gets created:

6. Set Visual Studio Project Properties

In your Visual Studio project select Configuration properties-> linker-> input-> additional dependencies and enterLibboost_thread-vc100-mt-gd-1_46_1.lib:

In your Visual Studio project set the project configuration properties-> linker-> General-> additional include directories, telling it the location of the stage/lib Folder:

And that's it! This example boost thread program shoshould now compile and run giving the following output:

Main: startup

Main: waiting for Thread

WORKER: Running

WORKER: finished

Main: Done

License

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.