【问题标题】:Starting a member function with arguments in a separate thread在单独的线程中使用参数启动成员函数
【发布时间】:2021-06-12 10:00:19
【问题描述】:

我有一个成员函数MyClass::doStuff(QString & str)

我正在尝试从这样的线程调用该函数:

std::thread thr(&MyClass::doStuff, this);

但是,这会导致错误:

/usr/include/c++/4.8.2/functional:1697: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (MyClass::*)(QString&)>(MyClass*)>’ typedef typename result_of<_Callable(_Args...)>::type result_type;

所以我试图给它一个论点:

QString a("test");
std::thread thr(&MyClass::doStuff(a), this);

但是,这会导致此错误:lvalue required as unary ‘&amp;’ operand

我将如何从单独的线程中运行带有参数的成员函数?

【问题讨论】:

    标签: c++ multithreading qt stdthread


    【解决方案1】:

    只需将参数添加到线程的构造函数中:

    QString a("test");
    std::thread thr(&MyClass::doStuff, this, a);
    

    由于您的函数接受 reference,您应该像这样使用std::ref()

    MyClass::doStuff(QString& str) { /* ... */ }
    
    // ...
    
    QString a("test");
    std::thread thr(&MyClass::doStuff, this, std::ref(a)); // wrap references
    

    【讨论】:

    • 问题:由于doStuff 要求提供参考,但它在另一个线程中运行,没有什么能阻止你删除a,你最终会得到一个UB?
    • @Ceros 当然,如果你删除了其他正在使用的变量,那就麻烦了:)
    • 这种情况下std::ref的目的是什么?
    • 看来是为了模板类型推导
    • @Ceros 将变量放入std::reference_wrapper en.cppreference.com/w/cpp/utility/functional/ref 传递被接受为引用的参数时需要它。
    猜你喜欢
    • 2021-09-25
    相关资源
    最近更新 更多