boost线程池的先进先出、后进先出、优先级

#include <./boost/threadpool.hpp>

using namespace std;
using namespace boost::threadpool;


//
// Helpers
boost::mutex m_io_monitor;

void print(string text)
{
boost::mutex::scoped_lock lock(m_io_monitor);
cout << text;
}

template<typename T>
string to_string(T const & value)
{
ostringstream ost;
ost << value;
ost.flush();
return ost.str();
}



//
// An example task functions
void task_1()
{
Sleep(3000);
print("  task_1()\n");
}

void task_2()
{
Sleep(3000);
print("  task_2()\n");
//Sleep(10000);
}

void task_3()
{
print("  task_3()\n");
}

int task_4()
{
print("  task_4()\n");
return 4;
}

void task_with_parameter(int value)
{
Sleep(3000);
print("  task_with_parameter(" + to_string(value) + ")\n");
}

int loops = 0;
bool looped_task()
{
print("  looped_task()\n");
return ++loops < 5; 
}


int task_int()
{
print("  task_int()\n");
return 23;
}


void fifo_pool_test()
{
pool tp;

tp.schedule(&task_1);
tp.schedule(boost::bind(task_with_parameter, 4));

if(!tp.empty())
{
tp.clear();  // remove all tasks -> no output in this test
}

size_t active_threads   = tp.active();
size_t pending_threads  = tp.pending();
size_t total_threads    = tp.size();

size_t dummy = active_threads + pending_threads + total_threads;
dummy++;

tp.size_controller().resize(5);
tp.wait();
}

void lifo_pool_test()
{
lifo_pool tp;
tp.size_controller().resize(0);
schedule(tp, &task_1);
tp.size_controller().resize(10);
tp.wait();
}

void prio_pool_test()
{
prio_pool tp(2);
schedule(tp, prio_task_func(1, &task_1));
schedule(tp, prio_task_func(10,&task_2));
    tp.schedule(prio_task_func(3000,boost::bind(task_with_parameter, 4)));
tp.schedule(prio_task_func(3500,boost::bind(task_with_parameter, 5)));
tp.schedule(prio_task_func(3600,boost::bind(task_with_parameter, 6)));
tp.schedule(prio_task_func(3900,boost::bind(task_with_parameter, 9)));
tp.schedule(prio_task_func(5000,boost::bind(task_with_parameter, 10)));
    tp.schedule(prio_task_func(8000,boost::bind(task_with_parameter, 11)));
    tp.schedule(prio_task_func(3000,boost::bind(task_with_parameter, 4)));
tp.schedule(prio_task_func(3500,boost::bind(task_with_parameter, 5)));
tp.schedule(prio_task_func(3600,boost::bind(task_with_parameter, 6)));
tp.schedule(prio_task_func(3900,boost::bind(task_with_parameter, 9)));
tp.schedule(prio_task_func(5000,boost::bind(task_with_parameter, 10)));
tp.schedule(prio_task_func(8000,boost::bind(task_with_parameter, 11)));
tp.schedule(prio_task_func(3000,boost::bind(task_with_parameter, 4)));
tp.schedule(prio_task_func(3500,boost::bind(task_with_parameter, 5)));
tp.schedule(prio_task_func(3600,boost::bind(task_with_parameter, 6)));
tp.schedule(prio_task_func(3900,boost::bind(task_with_parameter, 9)));
tp.schedule(prio_task_func(5000,boost::bind(task_with_parameter, 10)));
tp.schedule(prio_task_func(8000,boost::bind(task_with_parameter, 11)));
tp.schedule(prio_task_func(3000,boost::bind(task_with_parameter, 4)));
}


void future_test()
{
fifo_pool tp(5);
future<int> fut = schedule(tp, &task_4);
int res = fut();
}


int main (int , char * const []) 
{
//fifo_pool_test();
//lifo_pool_test();
prio_pool_test();
//future_test();
system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值