Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template function will not compile when called as a thread

I have a problem relating to template functions and threads:

template <class TYPE_size>
void Threader(TYPE_size counter)
{
    counter++;
}
int main()
{
    unsigned int counter = 100;
    thread one(Threader,counter);
    one.join();    
    cout << counter;
}

This does not compile; I get:

error: no matching function for call to âstd::thread::thread(, unsigned int&)â

If I remove the template it compiles and if I change the function call to a standard function call rather than a thread (still using the template) it compiles.

Does anyone know why this is?

I'm using Centos5 64 bit.

 error: no matching function for call to âstd::thread::thread(<unresolved overloaded function type>, unsigned int&)â
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:124: note: candidates are: std::thread::thread(std::thread&&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:122: note:                 std::thread::thread(const std::thread&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:121: note:                 std::thread::thread()
like image 663
Columbo Avatar asked Dec 01 '11 14:12

Columbo


2 Answers

Your missing the argument list of your template.

Try:

 unsigned int counter = 100;
 thread one(Threader<unsigned int>,counter);

or, if you are using c++x0/c++11 standard, give your template a standard type like:

template <typename TYPE_size = unsigned int>
void Threader(TYPE_size counter)
{
    counter++;
}
int main()
{
    unsigned int counter = 100;
    thread one(Threader<>,counter);
    one.join();    
    cout << counter;
}
like image 195
Michael Haidl Avatar answered Sep 27 '22 21:09

Michael Haidl


C++11 introduced lambdas, this can be used in this case as well.

Basically, the thread is created with the use of a lambda, where the lambda calls the function that then allows template type deduction to take place.

thread one([counter]() { Threader(counter); });

Above, the counter is captured by value, but as some of the answer suggest, a capture by reference can also be used

#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 was flagged as a duplicate, hence the addition with the use of newer language features.

like image 26
Niall Avatar answered Sep 27 '22 22:09

Niall