Lambdas was introduced in C ++ 11, this can also be used in this case.
Basically, a thread is created using a lambda, where the lambda calls a function, which then allows the conclusion of a template type.
thread one([counter]() { Threader(counter); });
Above, the counter is fixed by value, but, as follows from some answer, you can use capture by reference
#include <iostream> #include <thread> template <class T> void Threader(T& counter) { counter++; } int main() { unsigned int counter = 100; std::thread one([&counter]() { Threader(counter); }); one.join(); std::cout << counter; }
Note: this question has been marked as duplicate, therefore adding using new language features.
source share