Threading in C++11

Threading is sooo easy in C++11. It’s done 1 single line :3


thread t(&MeraClass::sayHi,this,"Nayar");

That’s it. Use t.join() anywhere you want it to complete 😀

The first argument is the name of the function or method you want to call. Since i mostly code using Object Oriented Programming (OOP), i need to specify the class name then pass the object from which i’m creating the thread. Here could be the class:-


using namespace std;
class MeraClass {

public:
   MeraClass(){
       thread t(&MeraClass::sayHi,this,"Nayar");
       t.join();
   }

   void sayHi(string name){
        cout << "Hi "<< name <<endl;
   }
};

Hey Java, BURN!!!! 3:)

Leave a Reply

Your email address will not be published. Required fields are marked *