0

According to the way i understand threads, programming a multi-threaded program either can speed up the program or just match the execution time of a single-threaded version of the program.

So, my two questions are:

1- Is what i said true or false ?

2- give example when multi-threading code produce negative results on performance ?

Udai F.mHd
  • 82
  • 1
  • 7

2 Answers2

1

A multi-threaded program can behave worse than a single-threaded equivalent. This is primarily due to:

  • The cost to initially create additional threads
  • The cost of context switches
  • The potential for false sharing.

Creating many threads that do a small amount of work that alters adjacent regions of memory would likely exhibit all of those issues.

Multi-threaded programs in general introduce additional complexity and present numerous opportunities for bugs. That should also be taken into account when deciding whether or not to use multiple threads.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

1-True in general, false in rare cases. 2-example:

you have only one CPU core while no blocking/wait on any thread. There is necessarily waste of CPU time to coordinate multi threads. Since only one thread can run although they all could, (hence the non-blocking/non-waiting condition), the total time is higher.

user2023577
  • 1,752
  • 1
  • 12
  • 23