简体   繁体   中英

C++11 cmake O3 option <no matching constructor for initialization of 'std::thread'>

I use C++ with CMake makefile with -std=c++11 .

My program use several thread method. I can build and execute my program without problem.

But when I add -03 optimization flag on CMake option, I have this error message:

"no matching constructor for initialization of 'std::thread'"

First I d'ont understand why is appear only in -O3 option.

Second I would like to compile in -O3 , I see others Q&A who talk about mythread = std::thread(&X::run<A, B>, this, a, b); but it doesn't work in my program and I don't understand how to use.

Here my function I want to take into thread:

static void getPoints(Mat *in, vector<Point> *posPoint, float *h,int rad, int dex,int decy, Mat *debug = NULL );

Today I call very simply with: std::thread t1(get4points,&myImage, ...

In case of std::thread(&X::run<A, B>, this, a, b); I don't understand what is exactly &X::run<A, B> , in case of I call a function of one class in a function of the same class.

Example pseudo code:

class myclass
{
    template<int A, int B> void run(int a, int b)
    {
        // ...
    }

    void myMainfunction(int a, int b)
    {
        ?????? -> std::thread(&this::run<int, int>, this, 1, 1);
    }
};

From your code example you must define all your template parameters to specify the concrete function to pass in the thread constructor.

So if you have:

class myclass
{
   template<int A, int B> void run(int a, int b)
    {
    }
};

You have to specify the parameters for A & B like:

auto x = std::thread(&myclass::run<55, 66>, this, 1, 1);

If your method is static , there is no related object at all so it makes no sense to pass the object pointer to the thread constructor. You simple have to write:

class myclass
{
    template<int A, int B> static void run(int a, int b)
    {
    }
};


auto x = std::thread(&myclass::run<77, 88>, 1, 1);

You ask:

In case of std::thread(&X::run, this, a, b); I don't understand what is exactly &X::run, in case of I call a function of one class in a function of the same class.

You do not understand the difference of class and object! this points to an object of your class and not to a class itself. Start reading about the fundamentals of C++ before playing around with template stuff. In case of a static function, there is no object as already mentioned.

To get an idea if using the this pointer, the object and the call to non static functions take a look in this example:

class myclass
{   
    private:
        int ia; 

    public:
        myclass( int _ia): ia{_ia}{}

        template<int A, int B> static void staticFun(int a, int b)
        {   
            std::cout << "Val of A: " << A << " Val of B: " << B << " a: " << a << " b: " << b << std::endl;
        }   

        template<int A, int B> void Fun(int a, int b)
        {   
            std::cout << "Val of A: " << A << " Val of B: " << B << " a: " << a << " b: " << b << " ia of instance: " << ia << std::endl;
        }   
};  

int main() 
{   
    myclass mc1(1);
    myclass mc2(2);

    auto x = std::thread(&myclass::staticFun<55, 66>, 1, 2); 
    auto y = std::thread(&myclass::Fun<77,88>, &mc1, 3, 4); 
    auto z = std::thread(&myclass::Fun<78,89>, &mc2, 5, 6); 


    x.join();
    y.join();
    z.join();
}

output will be something like that:

Val of A: 55 Val of B: 66 a: 1 b: 2

Val of A: 77 Val of B: 88 a: 3 b: 4 ia of instance: 1

Val of A: 78 Val of B: 89 a: 5 b: 6 ia of instance: 2

But remember that calling the operator << of std::cout will no be synchronized at all. So each thread can write at any time into the stream and the result will be corrupted or in any order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM