gpt4 book ai didi

multithreading - 无法将参数传递给 std::thread?

转载 作者:行者123 更新时间:2023-12-04 06:51:17 24 4
gpt4 key购买 nike

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

我在哪里调用线程:

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 文件的无名命名空间中定义。

我收到以下错误:

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) {}
^

最佳答案

如果我这样做对我来说很好

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

关于multithreading - 无法将参数传递给 std::thread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com