Saturday, January 30, 2010

Creating Threads in C++ using Boost C++ Libraries

At present standard C++, does not support Threads. But there are other open source libraries available like ‘Boost C++’, which provides Threading feature along with many other features. This allows a C++ programmer to go ahead with developing applications that require threading support.

This article explains how to create a thread. Here we create two threads ‘ThreadA’ and ‘ThreadB’ which continuously outputs the text ‘ThreadA output’ and ‘ThreadB output’ to the console accordingly.

So we need to download and configure the Boost C++ libraries with visual studio.
Download Path: http://www.boost.org
Configuration Steps: http://technologicalthemes.blogspot.com/2009/08/configuring-boost-c-library-with-visual.html

After the configuration, create a project and include the following code.
Different parts of the code are explained appropriately, by adding comments in the code.

Main Function: main.cpp

#include"iostream"
#include"string"
#include"boost/thread/thread.hpp"
#include"boost/thread/mutex.hpp"
#include"boost/thread/condition.hpp"

#include"ThreadA.h"
#include"ThreadB.h"

using namespace std;

void main()
{
//Create an object of Thread-A
ThreadA threadA;

//Creating the thread: "ThreadA"
//Argument-1: Call 'thread_A_Function' function in Thread-A.
//Argument-2: Pass an instance of thread.
//NOTE: The two arguments are arguments to the BOOST C++ Thread
//Library, so that it starts the thread. So these two arguments are a must to
//create a thread.
boost::thread thrdA(&ThreadA::thread_A_Function, &threadA);

//As explained for Thread-A, same thing applies for Thread-B.
ThreadB threadB;
boost::thread thrdB(&ThreadB::thread_B_Function, &threadB);

//Initiates the Thread and waits for its completion.
thrdA.join();
thrdB.join();
}

Thread –A:

Header File: ThreadA.h

#ifndef THREADA_H
#define THREADA_H

#include"iostream"
#include"boost/thread/thread.hpp"

class ThreadA
{
public:
ThreadA(void);
~ThreadA(void);
void thread_A_Function();
};

#endif

CPP File: ThreadA.cpp

#ifndef THREADA_CPP
#define THREADA_CPP

#include "ThreadA.h"

ThreadA::ThreadA(void)
{
}

ThreadA::~ThreadA(void)
{
}

//Defining the 'thread_A_Function' Function.
void ThreadA::thread_A_Function()
{
for(;;)
{
std::cout<<"Thread A output"<

//Make the Thread sleep for 1 second.

boost::xtime xt; //create a timer object.
boost::xtime_get(&xt, boost::TIME_UTC); //initialize the timer
//-object to a standard time.
xt.sec += 1; //Set the delay in seconds.
boost::thread::sleep(xt); //make the thread sleep.
}
}

#endif

Thread -B:

Header File: ThreadB.h

#ifndef THREADB_H
#define THREADB_H

#include"iostream"
#include"boost/thread/thread.hpp"

class ThreadB
{
public:
ThreadB(void);
~ThreadB(void);
void thread_B_Function();
};

#endif

CPP File: ThreadB.cpp

#ifndef THREADB_CPP
#define THREADB_CPP

#include "ThreadB.h"

ThreadB::ThreadB(void)
{
}

ThreadB::~ThreadB(void)
{
}

//Defining the 'thread_A_Function' Function.
void ThreadB::thread_B_Function()
{
for(;;)
{
std::cout<<"Thread B output"<

//Make the Thread sleep for 1 second.

boost::xtime xt; //create a timer object.
boost::xtime_get(&xt, boost::TIME_UTC); //initialize the timer object
//to a standard time.
xt.sec += 1; //Set the delay in seconds.
boost::thread::sleep(xt); //make the thread sleep.
}
}

#endif

This example only works with the threading support provided by Boost C++ libraries. The upcoming proposed standard for C++, that is “C++0x” will include threading support.