一个并发程序开多少线程合适?

文章来源:http://blog.csdn.net/greencacti/article/details/5881964

最近在和朋友讨论的过程中,朋友说线程开得多性能好,理由是系统中已经有成百上千的线程,并发程序开的线程越多分配到CPU的概率越高。我不认可该观点,下面来验证。

最好起CPU核心数量*2 个线程
或者 CPU核心数量*2 +2 个线程

测试场景:

硬件:HP G6(8核16流水线)

操作系统:Suse10 SP2

 

测试程序实现描述:

定义一个long型数组,假设有n个Entry,当有m个线程时,线程1对0~(n/m-1)中元素依次循环累加,线程2对n/m~(2n/m-1)元素依次循环累加...。最后把long型数组所有entry相加,看得到的数(此数就是所有线程对数组操作的次数)

 

测试结果:

Thread Num   Result1                     Result2                     Result3

2   Threads:   186,793,032,169        186,697,212,695

4   Threads:   373,926,225,686        373,407,260,760

8   Threads:   742,705,309,211        746,706,667,311        744,690,656,181

16 Threads:   794,363,499,559

20 Threads:   794,068,481,342        793,703,559,671        794,294,479,450

测试结论:

1.当Active的线程数等于CPU流水线数时,系统性能最高;

2. 当然实际系统中有IO和网络操作时,很多时候瓶颈可能在IO;

3. 在设计系统时,要考虑到系统Active线程数,因为很多线程可能会进入block状态,不能一定认为线程数等于流水线就是性能最好;

测试程序:

#include
#include
#include

#define ARRAY_SIZE 1000000

struct range
{
    int start;
    int end;
};

long array[ARRAY_SIZE];
int threadStopFlag = 0;

void *updateArray(void * ptrRange);

int main(int argc, char* argv[])
{
    struct range *rangeArray[100] = {NULL};
    pthread_t threadIdArray[100];
    int numOfThreads = 0;
    int i = 0;
    int size = 0;
    int error = 0;
    long result = 0;

    //check the number of parameters
    if(2 != argc)
    {
        printf("usage: a.out threadnumber/n");
        return -1;
    }

    //validate the parameter
    numOfThreads = atoi(argv[1]);
    if(numOfThreads > 100)
    {
        printf("The number of threads is greater than 100/n");
        return -1;
    }   

    //create the worker thread
    size = ARRAY_SIZE / numOfThreads;
    for(i=0; i< numOfThreads; i++)
    {
        //calculate the start and end value
        rangeArray[i] = (struct range*)malloc(sizeof(struct range));
        if(NULL == rangeArray[i])
        {
            return -1;
        }
        
        rangeArray[i]->start = i * size;
        if(i != numOfThreads - 1)
        {
            rangeArray[i]->end = rangeArray[i]->start + size - 1;
        }
        else
        {
            rangeArray[i]->end = ARRAY_SIZE -1;
        }       

        //create the threads
        error = pthread_create(&threadIdArray[i], NULL, updateArray, (void *)rangeArray[i]);
        if(error != 0)
        {
            printf("pthread is not created./n");
            return -1;
        }
        
    }

    //kill all the worker threads
    sleep(300);
    threadStopFlag = 1;

    sleep(60);
    //free the malloc memory
    for(i=0; i < numOfThreads; i++)
    {
        free(rangeArray[i]);
        rangeArray[i] = NULL;
    }

    //calculate the total number
    for(i=0; i     {
        result += array[i];
    }
    printf("The total number is %ld/n", result);
    return 0;
}

void *updateArray(void * ptrRange)
{
    struct range *arrayRange;
    int pointer = 0;;
    int start = 0;;
    int end = 0;

    arrayRange = (struct range *)ptrRange;
    start = arrayRange->start;
    end = arrayRange->end;
    pointer = start;
    
    while(1)
    {
        if(pointer > end)
        {
            pointer = start;
        }

        array[pointer] += 1;
        pointer++;

        if(1 == threadStopFlag)
        {
            pthread_exit(0);
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。


你可能感兴趣的:(VC++/MFC)