无法将参数传递给std :: thread? [英] Can't pass parameters to std::thread?

查看:196
本文介绍了无法将参数传递给std :: thread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用std::thread.如许多示例所示,我的线程应该调用方法并传递struct作为参数.除了我非常简单的代码,否则无法编译.根据记录,我知道这个问题,但似乎没有任何帮助

I'm trying to use std::thread. My thread is supposed to call a method and pass a struct as a parameter, as so many examples show. Except my very simple code won't compile. For the record, I'm aware of this question but nothing there seems to help me.

我在哪里调用线程:

void Exporter::save() const {
    thread(write_to_disk, this->parameter).detach();
}

write_to_disk的签名:

void write_to_disk(const Parameter& parameter)

write_to_disk是在.cpp文件中的无名命名空间中定义的.

write_to_disk is defined in a nameless namespace in the .cpp file.

我收到以下错误:

src/Exporter.cpp:65:5: error: no matching constructor for initialization of 'std::__1::thread'
    thread(write_to_disk, this->parameter).detach();
    ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:374:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:263:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    thread(const thread&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:270:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}
^

推荐答案

如果可以的话,对我来说很好用

Works fine for me if I do

clang++ -std=c++11 test.cpp

这篇关于无法将参数传递给std :: thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆