# Getting started with multithreading (POSIX)

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

## 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;

// Wait for threads to finish


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 on other sites
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 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 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 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.

Regards
elFarto

##### 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 on other sites
Are you using a 2.6 kernel? Threading in linux prior to the 2.6 kernel kinda sucks...

1. 1
2. 2
3. 3
4. 4
JoeJ
20
5. 5
frob
20

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632214
• Total Posts
3004858

×