0

我正在尝试编写 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;
}`

和编译器输出 g

++ -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:

很奇怪。

这是我的第一个多线程任务,所以请给我一些建议,我该如何修复我的源代码。

4

1 回答 1

2

该错误源于tbb::make_filter试图复制Reader您传递的对象的事实。Reader是不可复制的,因为它拥有一个不可复制的std::fstream数据成员。您需要重新设计Reader,使其不持有std::fstream或传递std::reference_wrapper<Reader>.

于 2013-04-13T06:07:10.250 回答