I learning Thread Building Block with book "Pro TBB".
I want to rewrite to class function instead of using lambda expression as author wrote.
This is original source of the book, which I tested and it worked:
#include <iostream>
#include <tbb/tbb.h>
int main()
{
tbb::parallel_invoke(
[](){std::cout<<"Hello "<<std::endl;},
[](){std::cout<<"TBB! "<<std::endl;}
);
return 0;
}
But when I write as follow:
#include <iostream>
#include <tbb/tbb.h>
using std::cout;
using std::endl;
void print_function(const std::string &s)
{
cout<<s<<endl;
}
class printClass
{
private:
const std::string &myString;
public:
printClass(const std::string &s): myString{s} {};
void operator()(std::string s) const{
print_function(myString);
}
};
int main()
{
std::string s1 = "Hello", s2 = "TBB!";
tbb::parallel_invoke(
printClass(s1),
printClass(s2)
);
return 0;
}
It produced error:
In file included from /usr/local/include/tbb/tbb.h:61:0,
from figure_1_04_class.cpp:2:
/usr/local/include/tbb/parallel_invoke.h: In instantiation of ‘tbb::task* tbb::internal::function_invoker<function>::execute() [with function = printClass]’:
figure_1_04_class.cpp:30:1: required from here
/usr/local/include/tbb/parallel_invoke.h:47:24: error: no match for call to ‘(const printClass) ()’
my_function();
^
figure_1_04_class.cpp:17:10: note: candidate: void printClass::operator()(std::__cxx11::string) const
void operator()(std::string s) const{
^
figure_1_04_class.cpp:17:10: note: candidate expects 1 argument, 0 provided
I follow their example in chapter 2 in the book above. This is their example and it worked too:
#include <vector>
#include <tbb/tbb.h>
#include <iostream>
using std::cout;
using std::endl;
void print_fucntion(int v)
{
cout<<"v: "<< v<<endl;
}
void sidebar_pfor_lambda(int N, const std::vector<int> &a)
{
tbb::parallel_for(0, N, 1, [&a](int i)
{
print_fucntion(a[i]);
});
}
int main()
{
std::vector<int> v = {4, 5, 6, 7, 8};
sidebar_pfor_lambda(5, v);
return 0;
}
#include <vector>
#include <tbb/tbb.h>
#include <iostream>
using std::cout;
using std::endl;
void print_fucntion(int v)
{
cout<<"v: "<< v<<endl;
}
class Body
{
private:
const std::vector<int> &myVector;
public:
Body(const std::vector<int> &v) : myVector{v} {};
void operator()(int i) const {
print_fucntion(myVector[i]);
}
};
void sidebar_pfor_function(int N, const std::vector<int> &a)
{
tbb::parallel_for(0, N, 1, Body(a));
}
int main()
{
std::vector<int> v = {4, 5, 6, 7, 8};
sidebar_pfor_function(5, v);
return 0;
}
What am I doing wrong and how to fix it?
tbb::parallel_invoke
expects functional objects that can be invoked with zero arguments:
The expression
parallel_invoke(f0,f1,...,fk)
evaluatesf0()
,f1()
, ...,fk()
possibly in parallel.
Lambdas in your first example can be invoked in this way:
auto l = [](){ std::cout << "Hello" << std::endl; };
l(); // This is OK
But the functional object printClass
expects one argument:
std::string s1 = "Hello";
auto l = printClass(s1);
l(); // Not OK!
The compiler complains:
candidate expects 1 argument, 0 provided
The solution is pretty simple – remove the unnecessary argument (what is it there for?):
class printClass {
// ...
void operator()(/* std::string s */) const {
print_function(myString);
}
};