一个线程池的例子(三)

在Linux下,我们使用pthread线程库来进行线程编程,下面是一个线程池的简单例子,还望各位多多指教。

main.cpp

/*
 * main.cpp
 *
 *  Created on: 2014年12月14日
 *      Author: Richard
 */

#include <pthread.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <math.h>

using namespace std;

struct Task
{
    long m_StartValue;
    long m_EndValue;
    long m_MinResultValue;
    long m_MaxResultValue;
    long long m_SumResultValue;

    Task()
    {
        m_StartValue = 0;
        m_EndValue = 0;
        m_MinResultValue = 0;
        m_MaxResultValue = 0;
        m_SumResultValue = 0;
    }

    Task &operator=(const Task &T)
    {
        if (this != &T)
        {
            m_StartValue = T.m_StartValue;
            m_EndValue = T.m_EndValue;
            m_MinResultValue = T.m_MinResultValue;
            m_MaxResultValue = T.m_MaxResultValue;
            m_SumResultValue = T.m_SumResultValue;
        }

        return *this;
    }
};

vector<Task> g_TaskList;
pthread_cond_t g_CondFinished;
pthread_mutex_t g_TaskMutex;
pthread_cond_t g_CondStart;
int g_TaskCount = 0;

bool IsPrime(unsigned long n)
{
    if (n == 2)
    {
        return true;
    }

    if (n % 2 == 0 || n < 2)
    {
        return false;
    }

    long tmp = (int)sqrt((double)n);
    for (long i = 3; i <= tmp; i += 2)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

void CalCulatePrime(Task &T)
{
    long PrimesCount = 0;

    for (long i = T.m_StartValue; i < T.m_EndValue; i++)
    {
        if (IsPrime(i))
        {
            PrimesCount++;

            if (PrimesCount == 1)
            {
                T.m_MinResultValue = i;
            }

            T.m_SumResultValue = T.m_SumResultValue + (long long)i;
        }
    }

    PrimesCount = 0;
    for (long i = T.m_EndValue - 1; i >= T.m_StartValue; i--)
    {
        if (IsPrime(i))
        {
            PrimesCount++;

            if (PrimesCount == 1)
            {
                T.m_MaxResultValue = i;
                break;
            }
        }
    }
}

void *ThreadFunc(void *)
{
    pthread_mutex_lock(&g_TaskMutex);
    while (g_TaskCount == 0)
    {
        pthread_cond_wait(&g_CondStart, &g_TaskMutex);
    }
    pthread_mutex_unlock(&g_TaskMutex);

    while(1)
    {
        if (g_TaskList.size() == 0) {
            break;
        }

        pthread_mutex_lock(&g_TaskMutex);
        Task T = g_TaskList.at(0);
        g_TaskList.erase(g_TaskList.begin());
        pthread_mutex_unlock(&g_TaskMutex);

        CalCulatePrime(T);
        printf("ThreadID: %lu, Start: %ld, End: %ld, MinResult: %ld, MaxResult: %ld, SumResult: %lld\n",
            pthread_self(), T.m_StartValue, T.m_EndValue, T.m_MinResultValue, T.m_MaxResultValue,
            T.m_SumResultValue);

        pthread_mutex_lock(&g_TaskMutex);
        g_TaskCount--;

        if (g_TaskCount == 0)
        {
            pthread_cond_signal(&g_CondFinished);
            pthread_mutex_unlock(&g_TaskMutex);
            break;
        }

        pthread_mutex_unlock(&g_TaskMutex);
    }

    return NULL;
}

int main(int argc, char **argv) {

    pthread_mutex_init(&g_TaskMutex, NULL);
    pthread_cond_init(&g_CondStart, NULL);
    pthread_cond_init(&g_CondFinished, NULL);

    pthread_t ThreadID[8];
    for (int i = 0; i < 8; i++)
    {
        pthread_create(&ThreadID[i], NULL, ThreadFunc, NULL);
    }

    for (int i = 0; i < 50; i++)
    {
        Task T;
        T.m_StartValue = i * 1000;
        T.m_EndValue = (i + 1) * 1000 * 100;
        g_TaskList.push_back(T);
        g_TaskCount++;
    }

    pthread_mutex_lock(&g_TaskMutex);
    pthread_cond_broadcast(&g_CondStart);
    pthread_mutex_unlock(&g_TaskMutex);

    pthread_mutex_lock(&g_TaskMutex);
    while(g_TaskCount > 0)
    {
        pthread_cond_wait(&g_CondFinished, &g_TaskMutex);
    }
    pthread_mutex_unlock(&g_TaskMutex);

    for(int i = 0; i < 8; i++)
    {
        pthread_join(ThreadID[i], NULL);
    }

    pthread_mutex_destroy(&g_TaskMutex);
    pthread_cond_destroy(&g_CondFinished);
    pthread_cond_destroy(&g_CondStart);

    return 0;
}

运行截图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值