C++ 如何调试这个C++;11线程代码?

C++ 如何调试这个C++;11线程代码?,c++,c++11,C++,C++11,我最近开始用C++11编写代码。因为我理解了理论部分,所以我想通过编写代码来进行实验。这是我在C++中的第一个程序,它的作用是: 我正在创建一个类,它有一个线程类型的成员和两个成员函数,其中一个是线程函数,另一个是启动这个线程函数的函数。但我面临以下错误。我用谷歌搜索了很多次,但没有一个有用。我想产生2个“runThread”线程,它们将更新计数器。我知道计数器不同步,但一旦此错误得到解决,我将使用std::atomic变量处理此问题 #include <iostream> #inc

我最近开始用C++11编写代码。因为我理解了理论部分,所以我想通过编写代码来进行实验。这是我在C++中的第一个程序,它的作用是:

我正在创建一个类,它有一个线程类型的成员和两个成员函数,其中一个是线程函数,另一个是启动这个线程函数的函数。但我面临以下错误。我用谷歌搜索了很多次,但没有一个有用。我想产生2个“runThread”线程,它们将更新
计数器。我知道
计数器
不同步,但一旦此错误得到解决,我将使用
std::atomic
变量处理此问题

#include <iostream>
#include <stdio.h>
#include <thread>
#include <vector>

class Application {
public:
    Application(int val): counter(val) { printf("value is %d\n", counter); }
    void start();
    void print();
private:
    void runThread();
    int counter;
    std::vector<std::thread> thr;
};

void Application::runThread() {
    for(int i=0;1<50;i++)
        counter++;
}

void Application::start() {
    //std::thread t1(runThread);
    //std::thread t2(runThread);
    //this->thr.emplace_back(std::thread(&Application::runThread, this)); Tried this, but dint work
    //this->thr.emplace_back(std::thread(&Application::runThread, this));Tried this, but dint work
    thr.emplace_back(std::thread(runThread));
    thr.emplace_back(std::thread(runThread));
}

void Application::print() {
    printf("Counter = %d\n", counter);
}

int main(void)
{
    int a;
    Application app(300);
    app.start();
    std::cin >>a;
}

您需要包装呼叫,使用
lambda

std::thread([this](){this->runThread();})
或绑定函数:

std::bind(&Application::runThread, this)

您需要结束通话,请使用
lambda

std::thread([this](){this->runThread();})
或绑定函数:

std::bind(&Application::runThread, this)

编译器提示您使用的函数是非静态的,应该是。这是因为在C++中,即使你看不到,每个非静态的类成员函数都会得到一个附加的参数,它是一个<>代码>这个指针,你可能熟悉,这个指针指向函数然后运行的对象。正因为如此,您的
runThread()
函数实际上可以被视为
runThread(Application*this)
,这意味着
std::thread
构造函数不知道您要为该参数提供什么值,并希望您提供一个采用零参数的静态成员函数

一种解决方案是将
runThread()
设为静态,例如从全局变量(no)获取实例,或者实际告诉
std::thread()
构造函数参数是什么

std::thread(&Application::runThread, this)

编译器会提示您正在使用的函数是非静态的,应该是。这是因为在C++中,即使你看不到,每个非静态的类成员函数都会得到一个附加的参数,它是一个<>代码>这个指针,你可能熟悉,这个指针指向函数然后运行的对象。正因为如此,您的
runThread()
函数实际上可以被视为
runThread(Application*this)
,这意味着
std::thread
构造函数不知道您要为该参数提供什么值,并希望您提供一个采用零参数的静态成员函数

一种解决方案是将
runThread()
设为静态,例如从全局变量(no)获取实例,或者实际告诉
std::thread()
构造函数参数是什么

std::thread(&Application::runThread, this)

runThread
是一个非静态成员函数,这意味着它不能通过引用传递。相反,它必须被包装起来

替换

thr.emplace_back(std::thread(runThread));


另请参见:

runThread
是一个非静态成员函数,这意味着它不能通过引用传递。相反,它必须被包装起来

替换

thr.emplace_back(std::thread(runThread));


参见:

如果C++中的问题不包含“C”标记。<代码> STD::([此]({)-> -RunthRead())< /COD>不直接相关,但RunTrand方法将连续运行,因为您检查1是否小于50,而不是.i COSTYSTREF这是一个打印错误!!谢谢:)专业提示:标题只是为了有用的细节,应该简短。健谈的材料,就像明确的建议/帮助请求,或者你被困多久,都可以从问题中完全省略。谢谢如果问题是在C++中,不包括“C”标签。<代码> STD::Type([此]({-> -> RunthRead()))< /C>不直接相关,但是RunTrand方法将连续运行,因为您检查1是否小于50,而不是.i CONTYSTREF这是一个打印错误!!谢谢:)专业提示:标题只是为了有用的细节,应该简短。健谈的材料,就像明确的建议/帮助请求,或者你被困多久,都可以从问题中完全省略。谢谢<代码>标准::线程
比你想象的更聪明<代码>标准::线程thr(&应用程序::运行线程,this)就可以了;没有必要让显式的
std::bind
std::thread
比您认为的更聪明<代码>标准::线程thr(&应用程序::运行线程,this)就可以了;不需要显式的
std::bind