有人可以向我解释话题吗? [英] Can Someone Explain Threads to Me?

查看:58
本文介绍了有人可以向我解释话题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑向我的应用程序中添加线程过程以加快执行速度,但是问题是,老实说,我不知道如何使用线程或什么被认为是线程安全的".例如,游戏引擎如何在其渲染过程中利用线程,或者在什么情况下线程仅会被视为障碍?有人可以指出一些资源的途径,以帮助我了解更多信息或在此处进行解释吗?

I have been considering adding threaded procedures to my application to speed up execution, but the problem is that I honestly have no idea how to use threads, or what is considered "thread safe". For example, how does a game engine utilize threads in its rendering processes, or in what contexts would threads only be considered nothing but a hindrance? Can someone point the way to some resources to help me learn more or explain here?

推荐答案

这是一个非常广泛的主题.但是,如果我对线程一无所知,以下是我想知道的事情:

This is a very broad topic. But here are the things I would want to know if I knew nothing about threads:

  • 它们是单个进程中并行"发生的执行单位-这意味着处理器中的当前执行单位会快速切换.这可以通过不同的方法来实现.切换称为"上下文切换",与此相关的一些开销.

  • They are units of execution within a single process that happen "in parallel" - what this means is that the current unit of execution in the processor switches rapidly. This can be achieved via different means. Switching is called "context switching", and there is some overhead associated with this.

他们可以共享内存!这是可能发生问题的地方.我将在以后的要点中更深入地讨论这一点.

They can share memory! This is where problems can occur. I talk about this more in depth in a later bullet point.

并行化应用程序的好处在于,使用机器不同部分的逻辑可以同时发生.也就是说,如果您的进程的一部分是I/O绑定的,而一部分是CPU绑定的,则I/O密集型操作不必等到CPU密集型操作完成即可.如果您使用的是多核处理器,则某些语言还允许您同时运行线程(因此也可以并行执行CPU密集型操作),尽管并非总是如此.

The benefit of parallelizing your application is that logic that uses different parts of the machine can happen simultaneously. That is, if part of your process is I/O-bound and part of it is CPU-bound, the I/O intensive operation doesn't have to wait until the CPU-intensive operation is done. Some languages also allow you to run threads at the same time if you have a multicore processor (and thus parallelize CPU-intensive operations as well), though this is not always the case.

线程安全意味着没有竞赛条件,该术语用于当进程的执行取决于时间(某些您不希望这样做)时发生的问题.依靠).例如,如果您的线程AB都增加了共享计数器C,则可以看到A读取C的值,然后B读取,然后AC+1覆盖C,然后BC+1覆盖C.请注意,C实际上仅增加一次!

Thread-safe means that there are no race conditions, which is the term used for problems that occur when the execution of your process depends on timing (something you don't want to rely on). For example, if you have threads A and B both incrementing a shared counter C, you could see the case where A reads the value of C, then B reads the value of C, then A overwrites C with C+1, then B overwrites C with C+1. Notice that C only actually increments once!

避免争用情况的几种常见方法包括同步,它排除了对共享状态的相互访问,或者根本没有共享状态.但这只是冰山一角-线程安全是一个广泛的话题.

A couple of common ways avoid race conditions include synchronization, which excludes mutual access to shared state, or just not having any shared state at all. But this is just the tip of the iceberg - thread-safety is quite a broad topic.

我希望能对您有所帮助!可以理解,这是对需要大量学习的东西的快速介绍.我建议您以您喜欢的语言查找有关多线程的资源,无论发生什么情况,并对其进行详尽的阅读.

I hope that helps! Understand that this was a very quick introduction to something that requires a good bit of learning. I would recommend finding a resource about multithreading in your preferred language, whatever that happens to be, and giving it a thorough read.

这篇关于有人可以向我解释话题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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