Slow in multithread programming in C when using more than one thread
<p>I have written the following code. When I am using single thread (aa=1 in the code), it takes 11 seconds. But for aa=2, it takes 190 seconds. However my machine  has 4 cpus.  Will you kindly help me? I am compiling as </p>

<p>gcc  Thread.c   -lpthread -O3</p>

<pre><code>#include&lt;stdio.h&gt;
#include&lt;pthread.h&gt;
#include &lt;time.h&gt;
#include&lt;stdlib.h&gt;



#define N 100

int aa;


unsigned long long *reg, *b;
#define  LIMIT 5000000




void *abc(void* arg){
       srand(time(NULL));

       unsigned long long i,l;
       unsigned char   z,j;
       int *limit_ptr=(int *)arg;
       int i1=*limit_ptr;
       printf("i1=%d\n",i1);
       for(l=0;l&lt;LIMIT;l++){
           for(i=0;i&lt;N;i++)
               z=(((double) N)*rand()/(RAND_MAX+1.0));;

           z = (((double) N)*rand()/(RAND_MAX+1.0));;

           if(z==0)
               reg[i1]++;

        }           

}

int main(){

    int l;
    aa=2;
    printf("%d  \n\n",aa);
    reg = (unsigned long long *) malloc(aa * sizeof(unsigned long long ));
    b = (unsigned long long *) malloc(aa * sizeof(unsigned long long ));

    struct timespec start, finish;
    double elapsed;
    clock_gettime(CLOCK_MONOTONIC, &amp;start);

    for(l=0;l&lt;aa;l++)
        reg[l]=0;
    pthread_t tid[aa];

    int j1;
    unsigned long long  cc=0;



    for(j1=0;j1&lt;aa;j1++)
        b[j1]=j1;


    for(j1=0;j1&lt;aa;j1++){

        printf("%d\n", j1);
        pthread_create(&amp;tid[j1],NULL,abc,&amp;b[j1]);
    }
//    

    for(j1=0;j1&lt;aa;j1++){
        pthread_join(tid[j1],NULL);
        printf("%d\n", j1);
    }

    for(j1=0;j1&lt;aa;j1++)    
        cc=cc+reg[j1]; 


    printf("%0.10f   %llu\n", (double)cc/(aa*LIMIT),cc);

    clock_gettime(CLOCK_MONOTONIC, &amp;finish);
    elapsed = (finish.tv_sec - start.tv_sec);
    elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;  

    printf("Time=%0.10f\n", elapsed);  
    pthread_exit(NULL);

}
</code></pre>

<p>Based on suggestion, I have changed the code. Although execution time is ok now, but random number generation is not fine. Values of the array reg in the code are same. Please help me. </p>

<pre><code>#include&lt;stdio.h&gt;
#include&lt;pthread.h&gt;
#include &lt;time.h&gt;
#include&lt;stdlib.h&gt;
#include &lt;sched.h&gt;


#define N 100
#define aa 4



unsigned long long *reg, *b;
#define  LIMIT 5000000

int rgen()
{
    int xi;
    int seed;
    struct drand48_data drand_buf;
    double x;
    static i;

    if (i==0){
        seed = time(NULL);
        srand48_r(seed, &amp;drand_buf);
        i = 10;
    }

    drand48_r (&amp;drand_buf, &amp;x);
    xi = (int) (x * 100);      

    return xi;
}


void *abc(void* arg){
       int l,i, z;
       int *limit_ptr = (int *) arg;
       int i1 = *limit_ptr;

       printf("i1=%d\n",i1);
       for(l=0; l&lt;LIMIT; l++){
                 for(i=0; i&lt;N; i++)
                     z= rgen(i1);

           if(z==0)
               reg[i1]++;

        }           

}

int main(){

int l;

printf("%d  \n\n",aa);
reg = (unsigned long long *) malloc(aa * sizeof(unsigned long long ));
b = (unsigned long long *) malloc(aa * sizeof(unsigned long long ));

struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &amp;start);

for(l=0;l&lt;aa;l++)
    reg[l]=0;
pthread_t tid[aa];

int j1;
unsigned long long  cc=0;



for(j1=0;j1&lt;aa;j1++)
  b[j1]=j1;


for(j1=0;j1&lt;aa;j1++){

    printf("%d\n", j1);
    pthread_create(&amp;tid[j1],NULL,abc,&amp;b[j1]);
}
//    
  printf("created threads\n"); 
  for(j1=0;j1&lt;aa;j1++){
   pthread_join(tid[j1],NULL);
   printf("%d\n", j1);
   }

for(j1=0;j1&lt;aa;j1++)    
  cc=cc+reg[j1]; 

printf("\n");
for(j1=0;j1&lt;aa;j1++)   
   printf("%llu   ", reg[j1]);

printf("\n");

printf("%0.10f   %llu\n", (double)cc/(aa*LIMIT),cc);

clock_gettime(CLOCK_MONOTONIC, &amp;finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;  

printf("Time=%0.10f\n", elapsed);  
pthread_exit(NULL);


}
</code></pre>