vendredi 4 septembre 2020

why should the functor be placed inside the brackets while passing to thread costructor?

#include <iostream>
#include <thread>
class DisplayThread
{
public:
    void operator()()     
    {
        for(int i = 0; i < 10000; i++)
            std::cout<<"Display Thread Executing"<<std::endl;
    }
};
 
int main()  
{
   //line 1:
    std::thread threadObj( (DisplayThread()) );
    for(int i = 0; i < 10000; i++)
        std::cout<<"Display From Main Thread "<<std::endl;
    std::cout<<"Waiting For Thread to complete"<<std::endl;
    threadObj.join();
    std::cout<<"Exiting from Main Thread"<<std::endl;
    return 0;
}
    

In line 1: if i use like "threadObj(DisplayThread())", It gives an error saying no class type.

Could someone tell me why functor while passing to thread constructor has to be inside"()" braces?

Aucun commentaire:

Enregistrer un commentaire