简体   繁体   English

任何计算机(多核或单核)是否可以同时运行多个线程

[英]Can any computer (multi or single core) run many threads at the same time

i have simple but not easy question我有一个简单但不简单的问题

Can any computer (multi or single core) run many threads at the same time, the very same time, i mean two or more threads runs at the same time of execution ?任何计算机(多核或单核)都可以同时运行多个线程,同时,我的意思是两个或多个线程同时运行吗?

The capability of running multiple threads at the same time is the reason why multicore processors are being produced.在同一时间运行多个线程的能力就是正在生产多核处理器的原因。 It was getting more and more difficult to boost single-core CPU's efficiency due to various reasons.由于种种原因,提高单核CPU的效率变得越来越困难。 At the same time, as many threads as the number of cores/processors you have can be executed.同时,可以执行与您拥有的内核/处理器数量一样多的线程。 If you have multiple multicore processors, the maximum number of threads running in parallel will be num_cpus*cores_per_cpu.如果您有多个多核处理器,并行运行的最大线程数将为 num_cpus*cores_per_cpu。 If the number of threads exceeds the number of available cores, they will be executed interlaced.如果线程数超过可用内核数,它们将交错执行。

You can create multiple threads in your environment and have a single CPU core execute these threads.您可以在您的环境中创建多个线程,并让单个 CPU 内核执行这些线程。 However, their execution will not be in parallel but merely concurrent.但是,它们的执行将不是并行的,而只是并发的。 In other words, the CPU will execute one thread (in part or completely) first then the other thread.换句话说,CPU 将首先执行一个线程(部分或全部),然后执行另一个线程。 If there was work left on the first thread it then may return to the first thread again and execute some more instructions.如果第一个线程上还有工作,那么它可能会再次返回到第一个线程并执行更多指令。 The threads are thus progressing concurrently .线程因此同时进行 If the threads were executed on more than one CPU core then they can be executed at the same time which means they are executed in parallel .如果线程在多个 CPU 内核上执行那么它们可以同时执行这意味着它们是并行执行的。

In summary, multiple threads on a single CPU core can be executed concurrently.总之,单个 CPU 内核上的多个线程可以并发执行。 Multiple threads on multiple CPU cores can be executed concurrently or in parallel.多个 CPU 内核上的多个线程可以并发或并行执行。

See also this answer: Can multithreading be implemented on a single processor system?另请参阅此答案: 能否在单处理器系统上实现多线程?

...at the same time, the very same time, i mean ... at the same time of execution... ......同时,在同一时间,我的意思是......在执行的同时......

Sounds like you are struggling to find a formal way of asking your question.听起来您正在努力寻找一种正式的提问方式。

The words "at the same time" do not usually appear in formal discussions about concurrency. “同时”这个词通常不会出现在关于并发的正式讨论中。

When you're talking about time, there are events which are indivisible moments in time, and then there are intervals .当你谈论时间时,有些事件是不可分割的时刻,然后是间隔 Every interval is defined by its begin event and its end event .每个间隔由其开始事件结束事件定义

Whenever you're trying to prove the behavior of some system, and your proof revolves around events, A and B then either;每当你试图证明某个系统的行为时,你的证明都围绕着事件展开,然后 A 和 B 要么;

  • You know or can prove that A happens before B,你知道或可以证明 A 在 B 之前发生,
  • You know or can prove that B happens before A,你知道或可以证明 B 发生在 A 之前,
  • Your proof doesn't depend on the order in which they happen, or你的证明不取决于它们发生的顺序,或者
  • Your proof is going to fail.你的证明会失败。

Those are the only possibilities.这些是唯一的可能性。 If your proof is going to fail, then you won't be able to rescue it by saying "A and B happen at the same time ."如果您的证明将失败,那么您将无法通过说“A 和 B同时发生”来挽救它。

When your proof revolves around two intervals, I and J, then either;当您的证明围绕两个区间 I 和 J 进行时,则要么;

  • you know or can prove that I finishes before J begins, or你知道或可以证明我在 J 开始之前完成,或者
  • you know or can prova that J finishes before I begins, or你知道或可以证明 J 在我开始之前完成,或者
  • The two intervals overlap .这两个区间重叠

"Overlap" is the preferred word in formal discussions. “重叠”是正式讨论中的首选词。 So,...所以,...

...The execution of two or more threads can overlap on a single-processor system, but at any given instant in time, the single CPU will only be working on behalf of one of the threads. ...两个或多个线程的执行可以在单处理器系统上重叠,但在任何给定的时刻,单个 CPU 将仅代表其中一个线程工作。

You've to be aware of you're CPU capabilities first.您必须首先了解自己的 CPU 功能。

Use those to learn how many processes your CPU can handle.使用这些来了解您的 CPU 可以处理多少进程。

1. lscpu 
2. cat /proc/sys/kernel/threads-max
3. cat /proc/sys/kernel/pid_max
4. htop
5. top
6. cat /proc/cpuinfo
7. ulimit -a
8. ps -aeF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM