ort23
ort23

Reputation: 83

Thread performance in C++

i have this following code:

  for ( int i = 0  ; i < 100 ; i++)
    for (int j = 0 ; j < 80 ; j ++)
    { 
     ...
    }

i splitted it to 8 threads.

pthread_t thread1, thread2,thread3,thread4,thread5,thread6,thread7,thread8;
int rc1,rc2,rc3,rc4,rc5,rc6,rc7,rc8;



 struct threads 
 {
 ...
 }

 void *PrintHello(void *args)
 {
   for (int j = 0 ; j < 10 ; j ++)
    { 
    }

 }

  for ( int i = 0  i < 100 ; i ++)
  {
    rc1 = pthread_create(&thread1, NULL,PrintHello,threads);,
    pthread_join(thread1,NULL);
    rc2 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread2,NULL);
    rc3 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread3,NULL);
    rc4 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread4,NULL);
    rc5 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread5,NULL);
    rc6 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread6,NULL);
    rc7 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread7,NULL);
    rc8 = pthread_create(&thread1, NULL,PrintHello,threads);
    pthread_join(thread8,NULL);

  }

i think, second one should be faster than first one .But, second one behaves like that there is only one thread.In other words, The code that is not splitted and the code that is splitted run in same time. why do they have same run time, while second one has 8 thread, first one has one thread?

thanks in advance.

Upvotes: 0

Views: 152

Answers (3)

3dz9j56
3dz9j56

Reputation: 112

Additionally to @NathanOliver and @JustSid it is not really great to define 8 threads separatly and write 8 times the same code if they all do the same work. Much better to use something like

pthread_t threadlist[8];
int results[8];

To initialize it a simple for loop will do.

for(int i = 0; i < 8; i++)
    results[i] = pthread_create(&threadlist[i], NULL, PrintHello, threads);

And start the threads with

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

This reduces the code much. Maybe there are really slightly changes in runtime but a for loop with an int are only a few more assembler commands. I do not know if this is even measurable.

Upvotes: 0

JustSid
JustSid

Reputation: 25318

Besides of what @NathanOliver said, keep in mind that spawning a thread comes at a significant cost, so whatever work you intend the threads to do, it has to be more costly than the penalty you get for actually spawning all these threads. So if your PrintHello method actually does exactly that, you will probably still see a performance degradation versus the single threaded version. The usual way these costs are offset is to spawn a finite amount of thread at the very beginning and divide work as it becomes available among them.

Also, last but not least, if your PrintHello method actually does only that, ie printf("Hello\n") or similar, you will most likely not see a performance improvement no matter what, as printf() will most likely take a shared lock which will see huge contention with all of your threads repeatedly trying to take it.

The bottom line is, multithreading is great to improve performance, but it's not trivial. Most of the time just throwing threads at your code is not going to improve your performance at all, and in the worst case will actually degrade it. If you want to see speed ups, you should profile your code and look out for big tasks of work that can be easily divided among several works that can all work independently on a part of the result. Those kinds of work can very easily be multithreaded and can see a increased throughput over single threaded code.

Upvotes: 2

NathanOliver
NathanOliver

Reputation: 180415

pthread_join(thread1,NULL); is going to halt execution of the main thread until thread1 finishes. You need to move all of the pthread_joins to after you construct all of the threads so they can all run concurrently.

You are also using thread1 for every pthread_create(&thread1, NULL,PrintHello,threads);. You need to use the other threads as well

for (int i = 0 i < 100; i++)
{
    rc1 = pthread_create(&thread1, NULL, PrintHello, threads);
    rc2 = pthread_create(&thread2, NULL, PrintHello, threads);
    rc3 = pthread_create(&thread3, NULL, PrintHello, threads);
    rc4 = pthread_create(&thread4, NULL, PrintHello, threads);
    rc5 = pthread_create(&thread5, NULL, PrintHello, threads);
    rc6 = pthread_create(&thread6, NULL, PrintHello, threads);
    rc7 = pthread_create(&thread7, NULL, PrintHello, threads);
    rc8 = pthread_create(&thread8, NULL, PrintHello, threads);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);
    pthread_join(thread5, NULL);
    pthread_join(thread6, NULL);
    pthread_join(thread7, NULL);
    pthread_join(thread8, NULL);

}

Upvotes: 4

Related Questions