将boost :: bind与模板化对象一起使用

时间:2012-07-04 00:23:34

标签: c++ templates boost-thread boost-bind

我想传递给 boost :: bind 一个模板化对象,但 g ++ 总是会产生错误。我已经找到了如何传递模板化函数,但是可以传递模板化对象吗?。

这是代码。

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>

#include "tbb/tick_count.h"

#include <iostream>
#include <vector>

template<class T>
class LockedVector {
private:
  std::vector<T> v;
  boost::mutex vector_mutex;
public:
  LockedVector(void):v(){}
  LockedVector(const unsigned int& n){
    v.reserve( n );
  }
  void Push(const T& t) {
    boost::mutex::scoped_lock lock( vector_mutex );
    v.push_back(t);
  }
  void Print(std::ostream& O) {
    for(unsigned int i(0);i<v.size();++i){
      O << v[i] << " ";
    }
    std::cout << std::endl;
  }
};

template<class T>
void AddToGlobalVector(LockedVector<T>& lv, unsigned int& start, const unsigned int& end, double& time)  {
  tbb::tick_count t0( tbb::tick_count::now() );
  for(;
  start < end;
  ++start) {
   lv.Push( (T)start );
  }
  time = (tbb::tick_count::now()-t0).seconds();
};

int main(void) {
  double time_t1(0.0);
  double time_t2(0.0);
  LockedVector<unsigned int> global;
  boost::thread t1(&AddToGlobalVector, global, (unsigned int)1, (unsigned int)10, boost::ref(time_t1) );
  boost::thread t2(&AddToGlobalVector, global, (unsigned int)11, (unsigned int)20, boost::ref(time_t2) );
  t1.join();
  t2.join();
  global.Print(std::cout);
  std::cout << "First thread(seconds): " << time_t1 << std::endl;
  std::cout << "Second thread(seconds): " << time_t2 << std::endl; 
}

这是g ++输出:

main.cpp: In function ‘int main()’:
main.cpp:58: error: no matching function for call to ‘boost::thread::thread(<unresolved overloaded function type>, LockedVector<unsigned int>&, unsigned int, unsigned int, const boost::reference_wrapper<double>)’
/usr/include/boost/thread/detail/thread.hpp:199: note: candidates are: boost::thread::thread(boost::detail::thread_move_t<boost::thread>)
/usr/include/boost/thread/detail/thread.hpp:147: note:                 boost::thread::thread()
/usr/include/boost/thread/detail/thread.hpp:118: note:                 boost::thread::thread(boost::detail::thread_data_ptr)
/usr/include/boost/thread/detail/thread.hpp:108: note:                 boost::thread::thread(boost::thread&)
main.cpp:59: error: no matching function for call to ‘boost::thread::thread(<unresolved overloaded function type>, LockedVector<unsigned int>&, unsigned int, unsigned int, const boost::reference_wrapper<double>)’
/usr/include/boost/thread/detail/thread.hpp:199: note: candidates are: boost::thread::thread(boost::detail::thread_move_t<boost::thread>)
/usr/include/boost/thread/detail/thread.hpp:147: note:                 boost::thread::thread()
/usr/include/boost/thread/detail/thread.hpp:118: note:                 boost::thread::thread(boost::detail::thread_data_ptr)
/usr/include/boost/thread/detail/thread.hpp:108: note:                 boost::thread::thread(boost::thread&)

感谢。

修改

好吧,好吧。我找到了怎么做。

boost::thread t1(&AddToGlobalVector<unsigned int>, boost::ref(global), (unsigned int)1, (unsigned int)10, boost::ref(time_t1) );
boost::thread t2(&AddToGlobalVector<unsigned int>, boost::ref(global), (unsigned int)11, (unsigned int)20, boost::ref(time_t2) );

另外,我不得不将 boost :: mutex 移出 LockedVector 类,因为它不可移动并且 boost :: bind 抱怨。

现在的问题是,是否可以在类中维护互斥锁。

1 个答案:

答案 0 :(得分:0)

你可能想要将一个移动构造函数声明为LockedVector,它实例化一个新的互斥锁,而不是试图移动它。

LockedVector(LockedVector<T> && lv) : v(lv.v) {}

请注意,我在这里完全推测,因为我无法访问编译器。