コード例 #1
0
ファイル: test_topx.cpp プロジェクト: wangjzh/FindLogTop10k
// 直接依赖与全局变量是不好的做法,有时间的话可以改为传入参数的方式
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
0
ファイル: test_topx.cpp プロジェクト: wangjzh/FindLogTop10k
// 调用线程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);
}