c++ Boost Thread

c++ Boost Thread

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.

In this program ,the header file <thread.hpp> is used to create an executing thread.

//thread example
#include <iostream> #include <boost/thread.hpp> using namespace std; using boost::thread; void functr() { //do stuffs.. } void fun(int x) { //do stuffs.. } int main() { boost::thread first(functr); //new thread that calls functr(): boost::thread second(fun,0); //new thread that calls fun(0): std::cout<<"main ,functr and fun now execute concurrently..\n"; //synchronize threads: first.join(); //pauses until first finishes second.join(); //pauses until second finishes: std::cout<<"functr and fun completed.\n";

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.