简体   繁体   English

“std::thread”如何能够确定传递给构造函数的可变参数的数量

[英]How is 'std::thread' is able to determine the number of variadic arguments passed to the constructor

When calling the constructor of std::thread you pass a function and its desired arguments.在调用std::thread的构造函数时,您传递一个函数及其所需的参数。 How does std::thread determine the total number of arguments meant to be passed to the function? std::thread如何确定要传递给函数的参数总数?

#include <iostream>
#include <thread>

void task(int i, char c, bool b)
{
    return;
}

int main(int argc, char** argv)
{
    std::thread t(task, 10, 'c', true); // these 3 arguments
    t.join();
}

The std::thread constructor is implemented using a variadic template :std::thread构造函数是使用可变参数模板实现的

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

Where:在哪里:

  • Function is the type of callable object that will be invoked by the thread (whether that be a function, a lambda, a functor, etc). Function是线程将调用的可调用对象的类型(无论是函数、lambda、函子等)。
  • Args is the list of types in the args variadic parameter. Argsargs可变参数中的类型列表。
  • f is the actual callable object that the thread will invoke. f是线程将调用的实际可调用对象。
  • args is the list of values that will be passed to f . args是将传递给f的值列表。

std::thread is then able to forward args to f using parameter pack expansion , similar to f(args...); std::thread然后能够使用参数包扩展args转发到f类似于f(args...); . . The compiler itself, not std::thread , will expand args... to the actual values, ie: f(arg1, arg2, ..., argN) .编译器本身,而不是std::thread ,会将args...扩展为实际值,即: f(arg1, arg2, ..., argN)

So, std::thread t(task, 10, 'c', true);所以, std::thread t(task, 10, 'c', true); will create a worker thread that make a call similar to to f(args...) , which will have been expanded to task(10, 'c', true) .将创建一个工作线程,该线程进行类似于f(args...)的调用,该调用将被扩展为task(10, 'c', true)

Thus, the arguments you pass to the std::thread constructor must match the arguments of the f callable you pass in, otherwise the code fails to compile.因此,您传递给std::thread构造函数的参数必须与您传入的f可调用参数的参数匹配,否则代码将无法编译。

std::thread 's constructor has the form ofstd::thread的构造函数的形式为

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

As you can see, the first argument is the function to run, and the rest are the arguments to be passed to it.如您所见,第一个参数是要运行的函数,其余的是要传递给它的参数。 If those don't match, you get some sort of compiler error如果这些不匹配,你会得到某种编译器错误

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

相关问题 std::thread 如何存储通过其构造函数传递的可变参数? - How does std::thread store variadic arguments passed through its constructor? 在构造函数中使用可变参数初始化 std::tuple - Initialize std::tuple with variadic arguments in constructor 如何计算传递给可变参数宏的宏参数的数量? - How do I count the number of macro arguments passed to a variadic macro? 如何将传递给variadic宏的可变参数求和? - How to sum variadic arguments passed in to a variadic macro? 返回返回传递给它的参数数量的可变参数函数 - Variadic function that returns the number of arguments that are passed to it std 线程构造函数是否采用可变参数线程 function? - Does std thread constructor take variadic thread function? 如何存储传递给构造函数的可变参数模板参数,然后保存以供以后使用? - How to Store Variadic Template Arguments Passed into Constructor and then Save them for Later Use? 从传递给构造函数的参数类型推导可变参数成员类型 - Deducing variadic member type from types of arguments passed to constructor std :: bind如何增加传递给函数的参数数量? - How std::bind increases number of arguments passed to a function? 如何检查传递给variadic函数的参数的类型 - How to check the type of passed arguments to variadic function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM