提升线程池

发布于 2024-09-26 01:29:55 字数 4238 浏览 2 评论 0原文

我正在尝试使用 boost 线程池库创建一个线程池,但是在定义它时,我不断收到很多有关模板参数错误的错误。

我可能做了一些根本性错误的事情,但我没有看到它?

//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

I'm trying to create a threadpool with the boost threadpool library, but on defining it I keep getting lots of error about template params being wrong.

I'm probably doing something fundamentally wrong, but I'm not seeing it?

//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;

ERRORS:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-10-03 01:29:55

根据您的评论,tsk是一个变量。您不能将运行时表达式(例如变量)作为模板参数传递。模板完全是编译时的。

对于这个特定问题,您只需执行以下操作:

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

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

boost::threadpool::task_func 只是一个 boost::function0 — 任何 0-arg void 函数指针可以转换为该类型。

According to your comment, tsk is a variable. You can't pass runtime expressions (such as a variable) as template parameters. Templates are entirely compile-time.

For this specific problem, you just need to do this:

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

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

boost::threadpool::task_func is just a boost::function0<void> — any 0-arg void function pointer can convert to that type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文