我正在尝试编写wc的并行版本。我正在使用TBB库,但不幸的是出现了问题,我不知道如何解决错误。
我的代码
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<boost/lambda/lambda.hpp>
#include"include/tbb/pipeline.h"
using namespace std;
class Counter
{
private:
public:
unsigned int operator()(std::string const& str){
std::stringstream stream(str);
return std::distance(std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>());
} ;
};
class Reader
{
private:
std::fstream m_file;
string m_line;
public:
Reader(string &path):
m_line(""){
fstream file(path.c_str(), fstream::in);
};
std::string operator()(tbb::flow_control& fc){
if(m_file.eof()){
fc.stop();
m_file.close();
return m_line;
}
getline(m_file,m_line);
return m_line;
}
};
int main(int argc, const char* argv[] ){
unsigned int sum=0;
if(argc!= 3){
cout<<"podaj sciezke!"<<endl;
}else{
string path(argv[1]);
tbb::parallel_pipeline( /*max_number_of_live_token=*/16,
tbb::make_filter<void,string>(tbb::filter::serial_in_order, Reader(path))&
tbb::make_filter<string,unsigned int>(tbb::filter::parallel, Counter())&
tbb::make_filter<unsigned int,void>(tbb::filter::serial_in_order, [&](unsigned int x) {sum+=x;})
);
std::cout<<"word count "<<sum <<endl;
}
return 0;
}`
和编译器输出 克
++ -std=gnu++0x my_wc.cpp -o program -Xlinker -rpath -Xlinker lib/ia32/gcc4.4/ -L lib/ia32/gcc4.4/ -ltbb
In file included from my_wc.cpp:7:0:
include/tbb/pipeline.h: In constructor ‘tbb::interface6::internal::filter_node_leaf<T, U, Body>::filter_node_leaf(tbb::filter::mode, const Body&) [with T = void, U = std::basic_string<char>, Body = Reader]’:
include/tbb/pipeline.h:583:63: instantiated from ‘tbb::interface6::filter_t<T, U> tbb::interface6::make_filter(tbb::filter::mode, const Body&) [with T = void, U = std::basic_string<char>, Body = Reader]’
my_wc.cpp:49:80: instantiated from here
include/tbb/pipeline.h:554:77: error: use of deleted function ‘Reader::Reader(const Reader&)’
my_wc.cpp:20:7: error: ‘Reader::Reader(const Reader&)’ is implicitly deleted because the default definition would be ill-formed:
my_wc.cpp:20:7: error: use of deleted function ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’
In file included from my_wc.cpp:3:0:
/usr/include/c++/4.6/fstream:761:11: error: ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:774:11: error: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.6/istream:57:11: error: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.6/ios:45:0,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from my_wc.cpp:2:
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’
In file included from /usr/include/c++/4.6/iostream:40:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/ostream:57:11: error: ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/ostream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/ostream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from my_wc.cpp:3:0:
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’
/usr/include/c++/4.6/fstream:69:11: error: ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/streambuf:782:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
/usr/include/c++/4.6/fstream:69:11: error: within this context
In file included from my_wc.cpp:7:0:
include/tbb/pipeline.h: In member function ‘void* tbb::interface6::internal::concrete_filter<void, U, Body>::operator()(void*) [with U = std::basic_string<char>, Body = Reader]’:
my_wc.cpp:56:1: instantiated from here
include/tbb/pipeline.h:449:69: error: passing ‘const Reader’ as ‘this’ argument of ‘std::string Reader::operator()(tbb::interface6::flow_control&)’ discards qualifiers [-fpermissive]
include/tbb/pipeline.h: In member function ‘void* tbb::interface6::internal::concrete_filter<T, U, Body>::operator()(void*) [with T = std::basic_string<char>, U = unsigned int, Body = Counter]’:
my_wc.cpp:56:1: instantiated from here
include/tbb/pipeline.h:426:89: error: passing ‘const Counter’ as ‘this’ argument of ‘unsigned int Counter::operator()(const string&)’ discards qualifiers [-fpermissive]
特别是
行Reader::Reader(const Reader&)’ is implicitly deleted because the default definition would be ill-formed:
非常奇怪。
这是我的第一个多线程任务,所以请给我一些建议如何修复我的源代码。
答案 0 :(得分:2)
错误源于tbb::make_filter
试图复制您传递的Reader
对象的事实。 Reader
是不可复制的,因为它包含std::fstream
数据成员,该成员是不可复制的。您需要重新设计Reader
,使其不包含std::fstream
或传递std::reference_wrapper<Reader>
。