Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 在类中使用std::thread时出错_Multithreading_C++11 - Fatal编程技术网

Multithreading 在类中使用std::thread时出错

Multithreading 在类中使用std::thread时出错,multithreading,c++11,Multithreading,C++11,我尝试在VisualStudio2013中使用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:

我尝试在VisualStudio2013中使用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;
}
#包括“stdafx.h”
#包括
#包括
#包括//标准::计时::秒
#include//std::cout
#包括//std::thread,std::this\u thread::sleep\u for
#包括
无效线程_任务(int n){
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout c:\program files(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195):请参阅正在编译的函数模板实例化“void std::_Bind::operator()(void)”的参考
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192):编译类模板成员函数“unsigned int std::_LaunchPad::_Run(std:_LaunchPad*)”
1> 与
1>          [
1> _Target=std::_Bind
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187):请参阅正在编译的函数模板实例化“unsigned int std::_LaunchPad::_Run(std:_LaunchPad*)”的参考
1> 与
1>          [
1> _Target=std::_Bind
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205):请参阅正在编译的类模板实例化“std::\u LaunchPad”的参考
1> 与
1>          [
1> _Target=std::_Bind
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\thread(49):请参阅正在编译的函数模板实例化“void std::”的参考
1> 与
1>          [
1> _Target=std::_Bind
1>          ]
1> d:\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(29):请参阅正在编译的函数模板实例化“std::thread::thread(_-Fn&&,int&)”的参考
1> 与
1>          [
1> \u Fn=void(\uu thiscall MyClass::*)(int)
1>          ]
1>
是什么导致了这个问题? 谢谢@malchemist,当添加这个解决了这个问题。但是它让我感到困惑,这段代码仍然有一些编译错误,甚至像答案显示的那样编写


有人能帮我吗?非常感谢。

您必须在新线程中指定要调用函数的对象。请尝试以下操作:


threads.emplace_back(std::thread(&MyClass::thread_task,
,i+1));
您必须指定一个对象以在新线程内调用函数。请尝试以下操作:


threads.emplace_back(std::thread(&MyClass::thread_task,
,i+1));

可能重复的可能重复的可能重复的非常感谢,它肯定解决了这个问题。有可能在这样的gpu上运行吗?我遇到了一些像你回答的那样编写的代码,但我在编译时仍然出错。非常感谢,它肯定解决了这个问题。有可能在这样的gpu上运行吗?我遇到了一些编写的JU代码我不喜欢你的回答,但我在编译时仍然有错误。
#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>