It is a class supported in and on-ward c++11, to create & manage thread. This class provides it’s user to various forms of constructor to create thread, destructor and some built-in function to manage threads.
  • The task that we want to be performed by our thread, we must put that in a function. As we done in Here in our example “displayAcitviy”.
  • In a system, each thread is identified by a unique number known as Thread ID.
  • If there is no thread in program then it is called signal-threaded program. Because at lease our main() function is processed by scheduler and hence usually called as MAIN THREAD.
  • Thus our example have two thread (1) Created by t1 object (2) Main thread.

std::thread uses pthread library that is usually called as POSIX Library.

Here is the first program to create a thread. It would be very useful if you copy following code and try to compile it on your system or can you any online compiler.


#include <iostream>
#include<thread> //Thread Header
using namespace std;

//displayAcitviy- is the actual function that is being
//called by the thread.

void displayAcitviy()
int main()
    cout<<"Main:: Going to create thread "<<endl;
//Create thread
    std::thread t1(displayAcitviy);

    //join()- wait for thread t1 to complete its activity
    //exit Main
    cout<<"Main:: Exit"<<endl;

    return 0;

$ g++ FirstThread.cpp -o FirstThread –lpthread

//–lpthread is the threading library on linux, usually called POSIX library


Main:: Going to create thread
Main:: Exit

Your Comments /Suggestions & Questions are always welcome. 

We would like to help you with best of our knowledge.

So feel free to put Questions

No comments:

Post a Comment