gpt4 book ai didi

c++ - 成员函数线程

转载 作者:行者123 更新时间:2023-11-28 06:25:05 28 4
gpt4 key购买 nike

我正在尝试为以下函数创建线程

void SortingCompetition::masterSort(int low, int high)

像这样:

  thread a(&SortingCompetition::masterSort,this, low, j-1);
thread b (&SortingCompetition::masterSort,this, j+1,high);

并得到以下错误。

sortingcompetition.cpp:55:16: error: no matching constructor for initialization
of 'std::__1::thread'
thread b (&SortingCompetition::masterSort,this, j+1,high);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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 4 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 4 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 4 were
provided
thread() _NOEXCEPT : __t_(0) {}

我是线程的新手,所以我不确定该怎么做。

最佳答案

你必须 bind方法如下:

thread a(std::bind(&SortingCompetition::masterSort, this, std::placeholders::_1, std::placeholders::_2),low,j-1);

您还可以使用函数或静态方法。原因是,由于绑定(bind) this 指针被保留。要调用非静态实例的方法,您需要 this 指针。使用 std::bind 就可以做到这一点。 std::bind 现在需要该方法有多少个参数,这就是占位符发挥作用的地方。

关于c++ - 成员函数线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28661277/

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