我尝试在Visual Studio 2013中使用std :: thread。它可以像这样正常工作。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <vector>
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::thread> threads;
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads.emplace_back( std::thread(thread_task, i + 1));
}
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t : threads) {
t.join();
}
std::cout << "All threads joined.\n";
return 0;
}
但是,当我尝试在Class中创建线程时,遇到了一些编译错误。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <vector>
class MyClass
{
public:
MyClass();
void thread_task(int n);
~MyClass();
private:
};
MyClass::MyClass()
{
std::vector<std::thread> threads;
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads.emplace_back(std::thread(&MyClass::thread_task, i + 1));
}
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t : threads) {
t.join();
}
std::cout << "All threads joined.\n";
}
void MyClass::thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
MyClass::~MyClass(){}
int _tmain(int argc, _TCHAR* argv[])
{
MyClass test();
return 0;
}
错误消息将复制到下面。
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2064: term does not evaluate to a function taking 1 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 MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' 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 MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' 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 MyClass::* )(int),void,MyClass,int>,int>::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 MyClass::* )(int),void,MyClass,int>,int>::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 MyClass::* )(int),void,MyClass,int>,int>
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 MyClass::* )(int),void,MyClass,int>,int>
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 MyClass::* )(int),void,MyClass,int>,int>
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 MyClass::* )(int),void,MyClass,int>,int>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1> ]
1> d:\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(29) : see reference to function template instantiation 'std::thread::thread<void(__thiscall MyClass::* )(int),int>(_Fn &&,int &&)' being compiled
1> with
1> [
1> _Fn=void (__thiscall MyClass::* )(int)
1> ]
1>
感谢@malchemist,在添加此功能时可以解决该问题。但这让我感到困惑,即使是像答案所示那样编写,这段代码错误仍然会有一些编译错误。
谁能帮我?非常感谢。
您必须指定一个对象才能在新线程内调用函数。试试这个:
threads.emplace_back(std::thread(&MyClass::thread_task,
this
, i + 1));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句