Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PThreads & MultiCore CPU on Linux

I am writing a simple application that uses Threads to increase the performance. The problem is, that this application runs fine on windows, using the 2 cores that my CPU has. But When I execute on Linux, It seems that only uses 1 Core.

I can't understand why this happens.

These is my code, 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;
}

Output on Linux:

Time Sequential: 50 ms
Time Concurrent: 1610 ms

Output on Windows:

Time Sequential: 50 ms
Time Concurrent: 30 ms
like image 736
IRTHUS Avatar asked Feb 21 '11 16:02

IRTHUS


People also ask

What are pthreads in C?

pthreads or POSIX threads are an implementation of the thread API for C/C++. It allows the spawning of new concurrent process flows and the multithreading system, which allows parallel and distributed processing. It does so by dividing the program into subtasks whose execution can be interleaved to run in parallel.

What is meant by pthreads?

Portable Operating System Interface for Computer Environments (POSIX) is an interface standard governed by the IEEE and based on UNIX®.

Are pthreads still used?

Yes, the pthreads library is still used for threading. There are some higher level libraries (boost, or if you have a c++ 11 compliant compiler, the standard library) that will also give you threading capabilities, although for somethings you will still need to fall back to the plain pthread call.

Is pthreads an API?

POSIX thread (pthread) libraries. The POSIX thread libraries are a standards based thread API for C/C++. It allows one to spawn a new concurrent process flow.


2 Answers

clock() works different on windows vs linux, so don't use that to measure time. On linux it measures CPU time, on windows it measures wall clock time. Ideally these would be the same in this test case, but you should use something consistant between the platforms to measure the time. e.g. gettimeofday()

rand() serializes your threads on linux. rand() holds an internal lock as to be thread safe. The rand() manpage states rand() is not threadsafe nor reentrant, however at least the code in recent glibc aquires a lock around the call. I'm not sure how windows handles this, either it's not thread safe at all, or it uses thread local variables.

Use rand_r on linux, or find some better CPU utilization function to measure.

void* function(void*)
{
    unsigned int seed = 42;
    int i=0;
    for(i=0; i<1110111; i++)
        rand_r(&seed);
    return 0;
}
like image 181
nos Avatar answered Sep 22 '22 15:09

nos


The problem is that Linux multi-threaded version or rand() locks a mutex. Change your function to:

void* function(void*)
{
    int i=0;
    unsigned rand_state = 0;
    for(i=0; i<1110111; i++)
        rand_r(&rand_state);
    return 0;
}

Output:

Time Sequential: 10 ms
Time Concurrent: 10 ms
like image 36
Maxim Egorushkin Avatar answered Sep 21 '22 15:09

Maxim Egorushkin