Description:Boost thread library allows the implementation of threads in programs.
Thread is a class to represent individual threads of execution.
A thread of execution is a sequence of instructions that can be executed concurrently with the other such sequences in multi-threading environments,while sharing a same address space.
These threads can be executed individually and generate distinct outputs.
HEADER FILES :
In this program ,the header file <thread.hpp> is used to create an executing thread.
PROGRAM:
//thread example
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
using boost::thread;
void functr()
{
}
void fun(int x)
{
}
int main()
{
boost::thread first(functr);
boost::thread second(fun,0);
std::cout<<"main ,functr and fun now execute concurrently..\n";
first.join();
second.join();
std::cout<<"functr and fun completed.\n";}
EXPLANATION:
main function(): two threads were created ,thread first and thread second and assigned them with the functions functr
and fun respectively.Then both the threads were executed using join() function.
join function(): join threads(it is a public member function).An initialized thread object represents an active thread
of execution;such a thread object is joinable ,and has a unique thread id.
OUTPUT:
Comments