当多线程是个坏主意时? [英] When multi-threading is a bad idea?

查看:58
本文介绍了当多线程是个坏主意时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对线程的理解,编写多线程程序既可以加快程序速度,也可以仅匹配程序的单线程版本的执行时间.

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- 我说的是真的还是假的?

1- Is what i said true or false ?

2- 举例说明多线程代码对性能产生负面影响?

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

推荐答案

多线程程序的行为可能比等效的单线程程序更糟糕.这主要是由于:

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.

这篇关于当多线程是个坏主意时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆