Thursday, August 25, 2011

Example for a threaded code in C++ using Boost (part 1).

My title Nowadays it seems universities only teach Java and students are oblivious to the complex but efficient world of unmanaged programming languages –programming languages which run natively on the hardware and are not dependent on CLR or JVM to run– such as C/C++. Recently I had the honour to tutor two new members in our team about multithreading capabilities of C++. I was not surprised when my students did not know that C++ standard does not include any specifications and built in support for multi-threading (C++ 0x will support natively but that’s more of an adaptation of Boost threads to C++'s standard). Boost is one of the more popular libraries which support threads (concurrent programming).
Since I’ve a strong belief that “doing it yourself is the best way to learn” so without going into the theory of multithreading (that I am sure you can learn elsewhere), I would like to put down steps to use boost library and make your first “threads” in C++.
For this article I am using visual studio 2010 (express), other multiplatform IDE’s such as code::blocks and eclipse for C++ can also be used following similar steps.
The desired output of this exercise is to setup the boost environment, setup a project in visual studio with the boost dependencies and most importantly to make a couple of threads in C++.
  1. Download the latest version of Boot library from http://www.boost.org/users/download/#releases
  2. Install Boost, I have installed it to “C:\boost_1_46_1”, which looks a like figure-1.
  3. Assuming that visual studio 2010 (express) is already installed, make a new Win32 console application and create an empty project. I’ve named my project as simpleBoostThreads.
  4. Now to setup the Boost library’s include paths right click on the project name and click on properties. On the property page click on “Configuration Properties” ->”C/C++” ->”General”. In the additional Include Directories field add the path of the library in my case it is “C:\boost_1_46_1\”. It should look like as in figure-2
  5. The last step in setting up the boost library is to add the library to the linker. To do that go to “Configuration Properties” ->”Linker” ->”General”. In the “Additional Library Directories” field add the path of the library in my case it is “C:\boost_1_46_1\lib”. It should look like as in figure-3
  6. Now add a new file main.cpp file in your project and write the following code snippet. It should look like in figure-4.

  7. #include < iostream >
    #include < boost/thread.hpp >
    using namespace boost;
    using std::cout;//adding the ostream's cout object from the std namespace

    void thread1()
    {
    cout<<"This is the first thread\n";
    }

    void thread2()
    {
    cout<<"This is the second thread\n";
    }

    int main()
    {
    thread th1(&thread1), th2(&thread2);
    th2.join();
    th1.join();
    }
  8. Now execute the program, and voila you have your first thread’s duo.
Download the project (open it up in Visual Studio 2010 and compile, make sure the boost paths are changed according to your PC)
This is my first article about multithreading using Boost library. I am hoping to contribute more by writing about more practical issues while using boost library in real projects.

1 comment:

Anonymous said...

excellent points altogether, you simply gained a new reader.
What might you recommend about your submit that you made some days in the past?
Any sure?

Feel free to surf to my page :: grindmattsehmichajordanbofu2.com