简体   繁体   English

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

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

I use C++ with CMake makefile with -std=c++11 . 我将C ++与CMake makefile一起使用-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: 但是,当我在CMake选项上添加-03优化标志时,我-03此错误消息:

"no matching constructor for initialization of 'std::thread'" “没有匹配的构造函数,无法初始化'std :: thread'”

First I d'ont understand why is appear only in -O3 option. 首先,我不理解为什么仅在-O3选项中出现。

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); 其次,我想在-O3进行编译,我看到其他人在Q&A上谈论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, ... 今天我用以下方法非常简单地调用: std::thread t1(get4points,&myImage, ...

In case of std::thread(&X::run<A, B>, this, a, b); 如果是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. 我不明白&X::run<A, B>到底是什么,如果我在同一类的函数中调用一个类的函数。

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: 您必须为A&B指定参数,例如:

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. 如果您的方法是static ,则根本没有相关的对象,因此将对象指针传递给线程构造函数没有任何意义。 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); 如果是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. 如果我在同一类的函数中调用一个类的函数,我不明白&X :: run到底是什么。

You do not understand the difference of class and object! 您不了解类和对象的区别! this points to an object of your class and not to a class itself. this指向您班级的对象,而不是班级本身。 Start reading about the fundamentals of C++ before playing around with template stuff. 在尝试使用模板之前,请先阅读有关C ++的基础知识。 In case of a static function, there is no object as already mentioned. 如果是static函数,则没有已提到的对象。

To get an idea if using the this pointer, the object and the call to non static functions take a look in this example: 为了了解是否使用this指针,该对象和对非静态函数的调用在以下示例中进行了介绍:

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 A值:55 B值:66 a:1 b:2

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

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

But remember that calling the operator << of std::cout will no be synchronized at all. 但是请记住,调用std :: cout的运算符<<完全不会同步。 So each thread can write at any time into the stream and the result will be corrupted or in any order. 因此,每个线程可以随时将其写入流中,结果将被破坏或以任何顺序出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CFLAGS = -std = c ++ 11 -O3 -Wall -pedantic在makefile中无法识别 - CFLAGS=-std=c++11 -O3 -Wall -pedantic not being recognized in makefile 没有匹配的构造函数来初始化&#39;std :: thread&#39; - no matching constructor for initialization of 'std::thread' vscode中的C++:错误:没有匹配的构造函数用于初始化'std :: thread' - C++ in vscode: error: no matching constructor for initialization of 'std::thread' C ++ 11 std :: thread给出错误:没有匹配函数来调用std :: thread :: thread - C++11 std::thread giving error: no matching function to call std::thread::thread C++11:正确的 std::array 初始化? - C++11: Correct std::array initialization? 具有聚合初始化的C ++ 11构造函数委派 - C++11 constructor delegation with aggregate initialization C ++ 11构造函数和运算符的统一初始化= - C++11 Uniform initialization for constructor and operator= C ++ 11默认构造函数中的引用初始化 - Reference initialization in C++11 default constructor 转换依赖于std :: vector初始化列表构造函数的C ++ 11代码 - Converting C++11 code that depends on std::vector initialization list constructor C ++ 11线程编译错误,删除了复制构造函数和std :: thread,为什么? - C++11 thread compilation error, deleted copy constructor and std::thread, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM