本文整理汇总了C++中ThreadPool::AddWork方法的典型用法代码示例。如果您正苦于以下问题:C++ ThreadPool::AddWork方法的具体用法?C++ ThreadPool::AddWork怎么用?C++ ThreadPool::AddWork使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadPool
的用法示例。
在下文中一共展示了ThreadPool::AddWork方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LogCount
// 直接依赖与全局变量是不好的做法,有时间的话可以改为传入参数的方式
void LogCount(vector<string>& datasrc, ThreadPool& threadpool)
{
cout << "start logcount" << endl;
time_t beginTime = time(0);
vector<string>::iterator first = datasrc.begin();
vector<string>::iterator last = datasrc.end();
vector<string>::size_type size = datasrc.size();
for (int i = 0; i < g_nThreadNum ; ++i)
{
ThreadArg* arg = new ThreadArg;
arg->i = i;
arg->pLogsCount = g_logscount;
arg->first = first + size/g_nThreadNum * i;
arg->last = first + size/g_nThreadNum * (i + 1);
if (i == g_nThreadNum - 1)
arg->last = last;
cout << "第" << i << "个线程:" << "count [" << arg->first - first << "," << arg->last - first << "]" << endl;
Work w;
w.function = thread_fun_count;
w.arg = (void *)arg;
threadpool.AddWork(w); // 向线程池添加任务
}
while(!threadpool.AllFree()) // 等待任务执行结束
sleep(1);
time_t countEndTime = time(0);
cout << "thread count time: " << countEndTime - beginTime << endl;
// g_logscount[n], 每次把 g_logscount[n -i - 1] 数据合入g_logscount[i];
int needSumNum = g_nThreadNum;
while (needSumNum > 1)
{
countSum(needSumNum, threadpool);
if (needSumNum % 2 == 0)
needSumNum = needSumNum / 2;
else
needSumNum = needSumNum / 2 + 1;
}
time_t threadSumTime = time(0);
cout << "thread count sum time : " << threadSumTime - countEndTime << endl;
}
示例2: countSum
// 调用线程count
// 直接依赖与全局变量是不好的做法,有时间的话可以改为传入参数的方式
void countSum(int num, ThreadPool& threadpool)
{
for (int i = 0; i < num/2; ++i)
{
ThreadArg2* arg = new ThreadArg2;
arg->pLogsCount = g_logscount;
arg->i = i;
arg->j = num - i -1;
Work w;
w.function = thread_fun_sum;
w.arg = arg;
threadpool.AddWork(w);
}
while(!threadpool.AllFree())
sleep(1);
}