2012-07-04 60 views
0

我想傳遞給boost :: bind模板對象,但g ++總是產生錯誤。我已經找到了如何傳遞模板化函數,但可以傳遞模板化對象嗎?使用boost :: bind與模板對象

這是代碼。

#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 ::互斥LockedVector類的,因爲它是不可移動和的boost ::綁定抱怨。

所以現在的問題是如果有可能在課堂上保持互斥。

+2

您正在使用什麼GCC和加速的版本?您的代碼(包含您的編輯)使用Clang 3.1(C++ 11模式)和Boost 1.49編譯。您可能需要更新您的工具和Boost庫。你應該發佈你得到的錯誤,而不是隻說「boost :: bind抱怨」。我們應該如何猜測這些錯誤是什麼? –

回答

0

您可能希望此舉構造申報LockedVector,實例化,而不是試圖將它移到一個新的互斥。

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

請注意,我在這裏完全推測,因爲我沒有訪問編譯器。

+1

它完成了!謝謝! –