I learning Thread Building Block with book : "Pro TBB" https://www.apress.com/gp/book/9781484243978.
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;
}
I'm not a c++ expert. What am i doing wrong and how to fix it?
Aucun commentaire:
Enregistrer un commentaire