2010-10-01 41 views
0

我想創建一個線程池與boost線程池庫,但在定義它我不斷收到很多有關模板params錯誤的錯誤。boost threadpool

我可能在做一些根本性的錯誤,但我沒有看到它?

//Threadpool 
typedef boost::threadpool::thread_pool< 
    boost::threadpool::task_func, 
    boost::threadpool::fifo_scheduler<tsk>, 
    boost::threadpool::static_size<boost::threadpool::fifo_pool>, 
    boost::threadpool::resize_controller<boost::threadpool::fifo_pool>, 
    boost::threadpool::wait_for_active_tasks<boost::threadpool::fifo_pool> 
> pool; 

錯誤:

------ Build started: Project: Trial, Configuration: Debug Win32 ------ 

Compiling... 
Trial.cpp 
error C2923: 'boost::threadpool::fifo_scheduler' : 'tsk' is invalid as template argument '#1', type expected 
     see declaration of 'tsk' 
error C3200: 'boost::threadpool::fifo_pool' : invalid template argument for template parameter 'SizePolicy', expected a class template 
error C3200: 'boost::threadpool::resize_controller<Pool>' : invalid template argument for template parameter 'SizePolicyController', expected a class template 
     with 
     [ 
      Pool=boost::threadpool::fifo_pool 
     ] 
error C3200: 'boost::threadpool::wait_for_active_tasks<Pool>' : invalid template argument for template parameter 'ShutdownPolicy', expected a class template 
     with 
     [ 
      Pool=boost::threadpool::fifo_pool 
     ] 
error C3200: 'boost::threadpool::fifo_pool' : invalid template argument for template parameter 'SizePolicy', expected a class template 
     see reference to class template instantiation 'boost::threadpool::thread_pool<Task,SchedulingPolicy,SizePolicy,SizePolicyController,ShutdownPolicy>' being compiled 
     with 
     [ 
      Task=boost::threadpool::task_func, 
      SchedulingPolicy=boost::threadpool::fifo_scheduler, 
      SizePolicy=boost::threadpool::fifo_pool, 
      SizePolicyController=boost::threadpool::resize_controller<boost::threadpool::fifo_pool>, 
      ShutdownPolicy=boost::threadpool::wait_for_active_tasks<boost::threadpool::fifo_pool> 
     ] 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(79) : error C3200: 'boost::threadpool::resize_controller<Pool>' : invalid template argument for template parameter 'SizePolicyController', expected a class template 
     with 
     [ 
      Pool=boost::threadpool::fifo_pool 
     ] 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(79) : error C3200: 'boost::threadpool::wait_for_active_tasks<Pool>' : invalid template argument for template parameter 'ShutdownPolicy', expected a class template 
     with 
     [ 
      Pool=boost::threadpool::fifo_pool 
     ] 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(91) : error C2059: syntax error : '<' 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(91) : error C2238: unexpected token(s) preceding ';' 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(92) : error C2059: syntax error : '<' 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(92) : error C2238: unexpected token(s) preceding ';' 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(111) : error C2146: syntax error : missing ';' before identifier 'size_controller' 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(111) : error C2501: 'boost::threadpool::thread_pool<Task,SchedulingPolicy,SizePolicy,SizePolicyController,ShutdownPolicy>::size_controller_type' : missing storage-class or type specifiers 
     with 
     [ 
      Task=boost::threadpool::task_func, 
      SchedulingPolicy=boost::threadpool::fifo_scheduler, 
      SizePolicy=boost::threadpool::fifo_pool, 
      SizePolicyController=boost::threadpool::resize_controller<boost::threadpool::fifo_pool>, 
      ShutdownPolicy=boost::threadpool::wait_for_active_tasks<boost::threadpool::fifo_pool> 
     ] 
c:\Program Files\boost\boost_1_44\boost\threadpool\pool.hpp(112) : warning C4183: 'size_controller': missing return type; assumed to be a member function returning 'int' 

---------------------- Done ---------------------- 

    Build: 0 succeeded, 1 failed, 0 skipped 
+2

「很多關於模板參數錯誤的錯誤」完全不是描述性的。 – 2010-10-01 13:35:33

+0

什麼是tsk的樣子? – dgnorton 2010-10-01 14:09:34

+0

void taskfunc(); boost :: threadpool :: task_func tsk = taskfunc; //任務 – 2010-10-01 14:16:19

回答

5

根據您的意見,tsk是一個變量。您不能將運行時表達式(如變量)作爲模板參數傳遞。模板完全是編譯時。

對於這個特定的問題,你只需要做到這一點:

typedef boost::threadpool::thread_pool<> pool; 
void taskfunc(); 

// later on... 
pool my_pool; 
my_pool.schedule(&taskfunc); 

boost::threadpool::task_func只是一個boost::function0<void> —任何0-ARG空函數指針可以轉換爲該類型。