This article is from the Threads Programming FAQ, by Bryan O'Sullivan email@example.com with numerous contributions by others.
There are many possible causes for performance problems in
multithreaded programs. Given the scope for error, all I can do is
detail a few common pitfalls briefly, and point you at the section of
this FAQ on books about multithreaded programming.
* You can probably discount the performance of the threads package
you are using almost straight away, unless it is a user-level
package. If so, you may want to try to find out whether your whole
process is blocking when you execute certain system calls.
Otherwise, you should look at your own code unless you have a very
strong reason for believing that there may be a problem with your
* Look at the granularity of your locks. If a single lock protects
too much data for which there is contention, you will needlessly
serialise your code. On the other hand, if your locks protect very
small amounts of data, you will spend too much time obtaining and
releasing locks. If your vendor is worth their salt, they will
have a set of tools available that allow you to profile your
program's behaviour and look for areas of high contention for
locks. Tools of this kind are invaluable.