Jump to content
  • Advertisement
Sign in to follow this  
VanKurt

Getting started with multithreading (POSIX)

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks! I'm having some trouble using the POSIX threads. Here's what I do: I have a very complicated computation problem, split it up in 4 threads (each one computing part of it on its own). Then I join the threads and compare their results. I hoped to get a huge speed benefit when running this on a multicore machine, but there is none!!! It takes about 90 seconds on a single, a dual and a quad core machine. So I took a look at the cpu usage. it seems that the 100% usage I get on the single core are evenly distributed when there are more cores (so on the quad core each core gets about 25% usgae). That would explain why there is no speed benefit... Here's the piece of code I use (nothing fancy, just creating and joining the threads):
// Run threads
    int  iret1, iret2, iret3, iret4;
    iret1 = pthread_create( &thread1, NULL, evalThreadLAH2, (void*) &ti1);
    iret2 = pthread_create( &thread2, NULL, evalThreadLAH2, (void*) &ti2);
    iret3 = pthread_create( &thread3, NULL, evalThreadLAH2, (void*) &ti3);
    iret4 = pthread_create( &thread4, NULL, evalThreadLAH2, (void*) &ti4);

    // Wait for threads to finish
    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);
    pthread_join( thread3, NULL);
    pthread_join( thread4, NULL);


Is there anything I have do to make the threads use the full cpu power? As you might have noticed I'm quite new to this kind of stuff so there might be a simple solution I overlooked so far Thanks for your help!!! PS: Some system information: Ubuntu 8.04, Code::Blocks IDE, C++, Intel Q6600 Processor (and some others)

Share this post


Link to post
Share on other sites
Advertisement
Do you have any kind of locking mechanisms in the function(s) doing the calculations? If so, you might have done it in such a way that the different threads must wait for each other while doing the calculations, which effectively reduces them to one thread instead of four, as only one thread at a time can work while the others must wait. If this is the case, try rethinking the design of the threads in such a way that they can work independently.

It could for example be that you, each time a calculation is finished, put the result into a container shared between the threads. If this is done often, the threads would have to wait often, which would delay them. This could be solved by using a temporary container in each thread for putting the results into, which is then added to the shared container only after the thread is fully finished.

Otherwise, I do not know what the problem might be, even if others might have ideas.

Maybe post more code if this does not solve the problem.

Share this post


Link to post
Share on other sites
What Lajnold said; also, a simple way to test whether the basic threading facilities work would be to spawn threads that are simply running endless loops.

Also, I assume that your program is really computational bound? If it's not, then it might be that the bottleneck is simply somewhere else.

Share this post


Link to post
Share on other sites
Hi!
Thanks for the replies! As far as I can see the threads are totally independant. They work on their very own memory chunks (input as well as output).
But since that's the only clue I've got right now I'll investigate this a little further...

(I'm also sure that the app is computationally bound, but good idea anyways) :-)

Share this post


Link to post
Share on other sites
Some OS runs all thread on single CPU. (for example FreeBSD 4 and less). Threads not help to increase speed, only another process.

Share this post


Link to post
Share on other sites
You could try replacing your pthread_create/join with fork's instead (this is the 'traditional' method of threading on Unix machines).

Regards
elFarto

Share this post


Link to post
Share on other sites
fork() creates a new process. The two resulting processes do not share memory by default, so this is not threading.

(Yes, it's possible that some CPU affinity setting is getting in the way, but that would be rather surprising...)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!