线程与流程 [英] Thread and Process

查看:79
本文介绍了线程与流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程的最佳定义是什么,什么是进程? 如果我调用一个函数,我怎么知道一个线程正在调用它或一个进程(或者我不理解它?!).这是在多核系统(四核)中.

What is the best definition of a thread and what is a process? If I call a function, how do I know that a thread is calling it or a process (or am I not understanding it??!). This is in a multi-core system (quadcore).

推荐答案

要添加的一件事是多核处理器如何处理此问题.将线程视为代码的顺序执行.

One thing to add is how does a multi-core processor handle this. Think of a thread as the sequential execution of your code.

CPU中的内核一次只能执行一个线程.因此,如果此线程由于程序正在等待I/O操作完成而被阻止,则该过程将被阻止(非常简化的示例:Word无响应).多线程允许我们同时执行多个代码路径. 相同时间"是个谎言,因为在内核中一次只能实际执行一个线程,但是CPU给每个线程分配了一些时间,因此好像所有这些线程都在一个线程中执行一样.同时.一个很好的例子是Word中的拼写检查器.

A core in a CPU can only execute one thread at a time. So if this thread is blocked because the program is waiting for an I/O operation to finish, the process is blocked (very simplified example: Word not responding). Multi-threading allows us to execute multiple code paths at the same time. "Same time" is a bit of a lie, since only one thread can actually execute at a time in a core, but the CPU gives some small chunk of time to each thread, so it appears as if all these threads are executing at the same time. A good example here is the spell checker in Word.

如果您有多个内核,则唯一的区别是,在N核CPU中,您可以同时执行N个线程.为了简化很多,线程属于哪个进程都没有关系.更简单地说,您希望性能提高N倍. :-D

If you have multiple cores, the only difference is that in an N-Core CPU you can have N threads executing at the same time. To simplify a lot, it doesn't matter what process the threads belong to. To simply even further, you'd expect a N times performance increase. :-D

这篇关于线程与流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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