简体   繁体   English

使用带有参数的类成员函数调用std :: thread构造函数

[英]Calling a std::thread constructor with a class member function that takes parameters

I know there exists this question and this one as well. 我知道存在这个问题, 一个也。 I have gone through both of them and haven't been able to solve my problem. 我已经经历了这两个问题,但无法解决我的问题。 I was able to run examples for class member functions that take zero arguments, but was not able to deduce what the constructor for the thread would look like in case the member function had arguments. 我能够为带有零参数的类成员函数运行示例,但无法推断出该成员函数带有参数的情况下线程的构造函数是什么样的。

I have a class A. A has a member function f that takes 2 parameters p1 and p2. 我有一个类A。A有一个成员函数f,它带有2个参数p1和p2。 I have instantiated an object a of class A. I want to call the function f in a thread. 我已经实例化了类A的对象a。我想在线程中调用函数f。

From cppreference.com : 来自cppreference.com

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );

is how I must write the constructor. 这就是我必须编写构造函数的方式。

I am having trouble deciphering the above definition. 我在解读以上定义时遇到了麻烦。 How can I call af(p1,p2) defined as 我怎样才能将af(p1,p2)定义为

void A::f(int p1, int p2)

in a thread ? 在线程中?

Simply pass the arguments: 只需传递参数:

std::thread t(&A::f, &bar, p1, p2);
t.join();

Note : Notice that we are passing the address of your bar object, otherwise the thread constructor would copy your object. 注意 :请注意,我们正在传递您的bar对象的地址,否则线程构造函数将复制您的对象。 By doing this you have to guarantee that your object outlives your thread. 通过这样做,您必须保证对象的寿命超过了线程的寿命。 Note, that the same is true for the other arguments. 注意,其他参数也是如此。 In case of the arguments you can simply use std::ref . 对于参数,您可以简单地使用std::ref

In general, as mentioned in my other post, the std::thread constructor is defined in terms of the INVOKE definition ( §20.8.2.1 ): 通常,正如我在其他文章中提到的那样, std::thread构造函数是根据INVOKE定义(第20.8.2.1节 )定义的:

Define INVOKE (f, t1, t2, ..., tN) as follows: 如下定义INVOKE(f,t1,t2,...,tN):

  • (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T; (t1。* f)(t2,...,tN)当f是指向类T的成员函数的指针并且t1是类型T的对象或对类型T的对象的引用或对t的引用从T派生的类型的对象;
  • ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous item; ((* t1)。* f)(t2,...,tN)当f是指向类T的成员函数的指针并且t1不是上一项中描述的类型之一时;
  • t1.*f when N == 1 and f is a pointer to member data of a class T and t 1 is an object of type T or a t1。* f,当N == 1并且f是指向类T的成员数据的指针并且t 1是类型T或a的对象
    reference to an object of type T or a reference to an object of a 对类型为T的对象的引用或对类型为T的对象的引用
    type derived from T; 从T派生的类型;
  • (*t1).*f when N == 1 and f is a pointer to member data of a class T and t 1 is not one of the types described in the previous item; (* t1)。* f,当N == 1且f是指向类T的成员数据的指针且t 1不是上一项中所述的类型之一时;
  • f(t1, t2, ..., tN) in all other cases. 在所有其他情况下f(t1,t2,...,tN)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在成员函数中调用类构造函数 - Calling Class Constructor in Member Function C ++ 11:在构造函数中使用线程初始化执行函数成员的类中的std :: thread - C++11: std::thread inside a class executing a function member with thread initialisation in the constructor 使用类构造函数在线程中启动成员函数 - Starting a member function in a thread using class constructor 在没有副本构造函数的对象的成员函数中启动std :: thread - Start std::thread in member function of object that does not have a copy constructor 在构造函数中调用类成员的构造函数 - calling constructor of a class member in constructor 使用参数C ++调用std :: thread函数时发生编译器错误 - compiler error when calling a std::thread function with parameters C++ 从基类构造函数调用派生类的成员函数 - Calling a member function of a derived class from the base class constructor 使用指向成员 function 的指针在另一个 class 中调用 class 构造函数 - Calling class constructor inside another class using a pointer to a member function std :: thread成员函数。 应该通过该指针访问类字段吗? - std::thread member function. Should class fields be accessed by this pointer? 了解std :: thread语义,并以worker函数作为类成员 - understanding std::thread semantic with worker function as class member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM