我试图为以下函数创建线程
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) {}
我是线程的新手,所以我不知道该怎么做。
答案 0 :(得分:1)
您需要bind方法,如:
thread a(std::bind(&SortingCompetition::masterSort, this, std::placeholders::_1, std::placeholders::_2),low,j-1);
备选您可以使用函数或静态方法。原因是,保留了this
指针的到期绑定。要调用非静态实例的方法,需要this
指针。使用std::bind
即可完成此操作。 std::bind
现在需要该方法有多少参数,即占位符进入游戏的位置。