gpt4 book ai didi

C++ 方法线程

转载 作者:行者123 更新时间:2023-11-30 00:49:10 26 4
gpt4 key购买 nike

我想为类中的方法创建一个线程。我写了这个:

class Program
{
public:
Program();
void render();
};

Program::Program()
{
thread t(render);
}

void Program::render()
{
cout << "Hello" << endl;
}

这给了我这个错误信息:

“错误 C3867:‘Program::render’:函数调用缺少参数列表;使用‘&Program::render’创建指向成员的指针”

当我改为这样写时:

thread t(&Program::render);

它说:

1>c:\program files(x86)\microsoft visual studio 12.0\vc\include\functional(1149) : error C2064 : term does not evaluate to a function taking 0 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer - to - function or reference - to - function that takes appropriate number of arguments
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::_Do_call<,>(std::tuple<>,std::_Arg_idx<>)' being compiled
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::operator ()<>(void)' being compiled
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>::operator ()<>(void)' being compiled
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\thread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Program::* )(void),void,Program,>,>>(_Thrd_t *,_Target &&)' being compiled
1> with
1>[
1> _Target = std::_Bind<true, void, std::_Pmf_wrap<void(__thiscall Program::*)(void), void, Program, >, >
1>]
1> c:\users\erik\documents\visual studio 2013\projects\c++gameengine\c++gameengine\program.cpp(16) : see reference to function template instantiation 'std::thread::thread<void(__thiscall Program::* )(void),>(_Fn &&)' being compiled
1> with
1>[
1> _Fn = void(__thiscall Program::*)(void)
1>]

不太明白哪里不对。你能解释一下并给我一个解决方案吗?

最佳答案

render 是一个非静态成员函数,所以它需要一个对象来调用它。您需要将它包装在一个小函数对象中,以便在 Program 上调用它。根据口味,有两种方便的方法可以做到这一点:

thread t(&Program::render, this);
thread t([this]{render();});

下一个问题是线程是构造函数的本地线程,因此会立即被销毁。您不能在未加入(等待线程完成)或分离线程对象的情况下销毁线程对象。所以要么让它成为成员(member),这样你以后就可以加入它;或在返回前将其分离。

关于C++ 方法线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29084372/

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