Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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
C++ c++;0x线程初始化_C++_Multithreading_C++11 - Fatal编程技术网

C++ c++;0x线程初始化

C++ c++;0x线程初始化,c++,multithreading,c++11,C++,Multithreading,C++11,可能重复: 如下图所示,在c++0x中使用类方法定义线程构造函数时,无法解析i get函数。我做错了什么 例如,如果我有 #include <thread> using namespace std; class A { public: void doSomething(); A(); } 试试这个: class A { public: void doSomething(); A() { thread t(&A::doS

可能重复:

如下图所示,在c++0x中使用类方法定义线程构造函数时,无法解析i get函数。我做错了什么

例如,如果我有

#include <thread>
 using namespace std;
class A
{
 public:
    void doSomething();
    A();
}
试试这个:

class A
{
 public:
   void doSomething();

   A()
   {
      thread t(&A::doSomething, this);
    }
};

注意:您需要在某个地方加入您的线程,例如:

class A
{
public:
   void doSomething()
   {
      std::cout << "output from doSomething" << std::endl;
   }

   A(): t(&A::doSomething, this)
   {
   }
   ~A()
   {
     if(t.joinable())
     {
        t.join();
     }
   }

private:
  std::thread t;  
};

int main() 
{
    A a;        
    return 0;
}
A类
{
公众:
无效剂量测定法()
{
std::cout试试这个:

class A
{
 public:
   void doSomething();

   A()
   {
      thread t(&A::doSomething, this);
    }
};

注意:您需要在某个地方加入您的线程,例如:

class A
{
public:
   void doSomething()
   {
      std::cout << "output from doSomething" << std::endl;
   }

   A(): t(&A::doSomething, this)
   {
   }
   ~A()
   {
     if(t.joinable())
     {
        t.join();
     }
   }

private:
  std::thread t;  
};

int main() 
{
    A a;        
    return 0;
}
A类
{
公众:
无效剂量测定法()
{

std::库蒂,这起作用了。如果我不通过“这个”会不会不起作用?除非soSomething是静态成员函数great!!这是响应。@Jimm,它接受函数,然后是要传递的参数列表。该函数需要一个实例。如果是静态的,为什么&a::doSomething而不是a::doSomething?Ty不起作用。如果我不传递“this”,它会不起作用吗?除非soSomething是静态成员函数great!!这是响应。@Jimm,它接受函数,然后是要传递的参数列表。该函数需要一个实例。如果是静态的,为什么不是&a::doSomething而不是a::doSomething?