Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to avoid for performance reasons in multithreaded code?

I'm currently reviewing/refactoring a multithreaded application which is supposed to be multithreaded in order to be able to use all the available cores and theoretically deliver a better / superior performance (superior is the commercial term for better :P)

What are the things I should be aware when programming multithreaded applications?

I mean things that will greatly impact performance, maybe even to the point where you don't gain anything with multithreading at all but lose a lot by design complexity. What are the big red flags for multithreading applications?

Should I start questioning the locks and looking to a lock-free strategy or are there other points more important that should light a warning light?

Edit: The kind of answers I'd like are similar to the answer by Janusz, I want red warnings to look up in code, I know the application doesn't perform as well as it should, I need to know where to start looking, what should worry me and where should I put my efforts. I know it's kind of a general question but I can't post the entire program and if I could choose one section of code then I wouldn't be needing to ask in the first place.

I'm using Delphi 7, although the application will be ported / remake in .NET (c#) for the next year so I'd rather hear comments that are applicable as a general practice, and if they must be specific to either one of those languages

like image 439
Jorge Córdoba Avatar asked Sep 09 '09 14:09

Jorge Córdoba


2 Answers

One thing to definitely avoid is lots of write access to the same cache lines from threads.

For example: If you use a counter variable to count the number of items processed by all threads, this will really hurt performance because the CPU cache lines have to synchronize whenever the other CPU writes to the variable.

like image 102
Zan Lynx Avatar answered Nov 16 '22 04:11

Zan Lynx


One thing that decreases performance is having two threads with much hard drive access. The hard drive would jump from providing data for one thread to the other and both threads would wait for the disk all the time.

like image 39
Janusz Avatar answered Nov 16 '22 04:11

Janusz