PThreads & MultiCore CPU в Linux

, я пишу простое приложение, использующее потоки для повышения производительности. Проблема в том, что это приложение отлично работает в Windows, используя 2 ядра, которые есть у моего процессора. Но когда я выполняю работу в Linux, кажется, что используется только 1 ядро.

Я не могу понять, почему это происходит.

Это мой код, C ++:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

void* function(void*)
{
    int i=0;
    for(i=0; i<1110111; i++)
        rand();
    return 0;
}

void withOutThreads(void)
{
    function(0);
    function(0);
}

void withThreads(void)
{
    pthread_t* h1 = new pthread_t;
    pthread_t* h2 = new pthread_t;
    pthread_attr_t* atr = new pthread_attr_t;

    pthread_attr_init(atr);
    pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);

    pthread_create(h1,atr,function,0);
    pthread_create(h2,atr,function,0);

    pthread_join(*h1,0);
    pthread_join(*h2,0);
    pthread_attr_destroy(atr);
    delete h1;
    delete h2;
    delete atr;
}

int main(void)
{
    int ini,tim;
    ini = clock();
    withOutThreads();
    tim = (int) ( 1000*(clock()-ini)/CLOCKS_PER_SEC );
    printf("Time Sequential: %d ms\n",tim);
    fflush(stdout);

    ini = clock();
    withThreads();
    tim = (int) ( 1000*(clock()-ini)/CLOCKS_PER_SEC );
    printf("Time Concurrent: %d ms\n",tim);
    fflush(stdout);
    return 0;
}

Вывод в Linux:

Time Sequential: 50 ms
Time Concurrent: 1610 ms

Вывод в Windows :

Time Sequential: 50 ms
Time Concurrent: 30 ms
13
задан Lightness Races with Monica 1 August 2011 в 18:18
поделиться