multithreading 同时运行两个线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19324306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running two threads at the same time
提问by user2644819
I want to know if a program can run two threads at the same time (that is basically what it is used for correct?). But if I were to do a system call in one function where it runs on thread A, and have some other tasks running in another function where it runs on thread B, would they both be able to run at the same time or would my second function wait until the system call finishes?
我想知道一个程序是否可以同时运行两个线程(这基本上是正确使用的?)。但是,如果我要在它在线程 A 上运行的一个函数中执行系统调用,并在另一个函数中运行其他一些任务,它在线程 B 上运行,它们是否能够同时运行,还是我的第二个函数等待系统调用完成?
Add-on to my original question: Now would this process still be an uninterruptable process while the system call is going on? I am talking about using any system call on UNIX/LINUX.
对我原来的问题的补充:现在这个过程在系统调用进行时仍然是一个不可中断的过程吗?我说的是在 UNIX/LINUX 上使用任何系统调用。
回答by ryyker
Multi-threading and parallel processing are two completely different topics, each worthy of its own conversation, but for the sake of introduction...
多线程和并行处理是两个完全不同的话题,每个话题都值得讨论,但为了介绍......
Threading:
When you launch an executable, it is running in a thread within a process. When you launch another thread, call it thread 2, you now have 2 separately running execution chains (threads) within the same process. On a single core microprocessor (uP), it is possible to run multiple threads, but not in parallel. Although conceptually the threads are often said to run at the same time, they are actually running consecutively in time slices allocated and controlled by the operating system. These slices are interleaved with each other. So, the execution steps of thread 1 do not actually happen at the same time as the execution steps of thread 2. These behaviors generally extend to as many threads as you create, i.e. packets of execution chains all working within the same process and sharing time slices doled out by the operating system.
线程:
当您启动可执行文件时,它正在进程内的线程中运行。当您启动另一个线程时,将其称为线程 2,您现在在同一进程中有 2 个单独运行的执行链(线程)。在单核微处理器 (uP) 上,可以运行多个线程,但不能并行运行。虽然概念经常被认为在运行的线程同时,它们实际上在操作系统分配和控制的时间片中连续运行。这些切片彼此交错。因此,线程 1 的执行步骤实际上并不与线程 2 的执行步骤同时发生。这些行为通常会扩展到与您创建的线程数量相同的线程,即执行链的数据包都在同一进程中工作并共享时间由操作系统分发的切片。
So, in your system callexample, it really depends on what the system call is as to whether or not it would finish before allowing the execution steps of the other thread to proceed. Several factors play into what will happen: Is it a blocking call? Does one thread have more priority than the other. What is the duration of the time slices?
因此,在您的系统调用示例中,在允许其他线程的执行步骤继续之前,它实际上取决于系统调用是否会完成。有几个因素会影响将要发生的事情:这是一个阻塞调用吗?一个线程是否比另一个线程具有更高的优先级。时间片的持续时间是多少?
Links relevant to threadingin C:
SO Example
POSIX
ANSI C
与C 中的线程相关的链接:
SO Example
POSIX
ANSI C
Parallel Processing:
When multi-threaded program execution occurs on a multiple core system (multiple uP, or multiple multi-core uP) threads can run concurrently, or in parallelas different threads may be split off to separate cores to share the workload. This is one example of parallel processing.
并行处理:
当多线程程序的执行中发生的多个核心系统(多了起来,或多个多芯UP)线程上可以同时运行,或者在平行作为不同的线程可以是分裂关闭以独立内核共享的工作量。这是并行处理的一个示例。
Again, conceptually, parallel processing and threading are thought to be similar in that they allow things to be done simultaneously. But that is concept only, they are really very different, in both target application and technique. Where threading is useful as a way to identify and split out an entire task within a process (eg, a TCP/IP server may launch a worker thread when a new connection is requested, then connects, and maintains that connection as long as it remains), parallel processing is typically used to send smaller components of the same task(eg. a complex set of computations that can be performed independently in separate locations) off to separate resources (cores, or uPs) to be completed simultaneously. This is where multiple core processors really make a difference. But parallel processing also takes advantage of multiple systems, popular in areas such as geneticsand MMORPGgaming.
同样,从概念上讲,并行处理和线程处理被认为是相似的,因为它们允许同时完成工作。但这只是概念,它们在目标应用和技术方面确实非常不同。线程作为识别和拆分进程内整个任务的一种方式很有用(例如,TCP/IP 服务器可以在请求新连接时启动一个工作线程,然后连接并保持该连接,只要它仍然存在) ),并行处理通常用于发送相同任务的较小组件(例如,可以在不同位置独立执行的一组复杂计算)分离到要同时完成的单独资源(核心或 uP)。这就是多核处理器真正发挥作用的地方。但是并行处理也利用了多个系统,这些系统在遗传学和MMORPG游戏等领域很受欢迎。
Links relevant to parallel processing in C:
OpenMP
More OpenMP(examples)
Gribble Labs - Introduction to OpenMP
CUDA Tookit from NVIDIA
与 C 中的并行处理相关的链接:
OpenMP
More OpenMP(示例)
Gribble Labs - NVIDIA OpenMP CUDA 工具包简介
Additional reading on the general topic of threading and architecture:
关于线程和架构的一般主题的附加阅读:
This summary of threading and architecture barely scratches the surface. There are many parts to the the topic. Books to address them would fill a small library, and there are thousands of links. Not surprisingly within the broader topic some concepts do not seem to follow reason. For example, it is not a given that simply having more cores will result in faster multi-threaded programs.
这个线程和架构的总结几乎没有触及表面。这个话题有很多部分。解决它们的书籍会填满一个小型图书馆,并且有数千个链接。毫不奇怪,在更广泛的主题中,一些概念似乎不符合理性。例如,并不是简单地拥有更多内核就会导致更快的多线程程序。
回答by Seg Fault
Yes, they would, at least potentially, run "at the same time", that's exactly what threads are for; of course there are many details, for example:
是的,它们至少有可能“同时”运行,这正是线程的用途;当然还有很多细节,例如:
If both threads run system calls that e.g. write to the same file descriptor they might temporarily block each other.
If thread synchronisation primitives like mutexes are used then the parallel execution will be blocked.
You need a processor with at least two cores in order to have two threads truly run at the same time.
如果两个线程都运行系统调用,例如写入相同的文件描述符,它们可能会暂时相互阻塞。
如果使用像互斥锁这样的线程同步原语,那么并行执行将被阻塞。
您需要一个至少有两个内核的处理器才能真正同时运行两个线程。
It's a very large and very complex subject.
这是一个非常庞大且非常复杂的主题。
回答by Khaula Fathima
If your computer has only a single CPU, you should know, how it can execute more than one thread at the same time.
In single-processor systems, only a single thread of execution occurs at a given instant. because Single-processor systems support logical concurrency, not physical concurrency.
On multiprocessor systems, several threads do, in fact, execute at the same time, and physical concurrency is achieved.
The important feature of multithreaded programs is that they support logical concurrency, not whether physical concurrency is actually achieved.
如果您的计算机只有一个 CPU,您应该知道它是如何同时执行多个线程的。
在单处理器系统中,在给定时刻只有一个执行线程发生。因为单处理器系统支持逻辑并发,而不是物理并发。
在多处理器系统上,多个线程实际上是同时执行的,并实现了物理并发。
多线程程序的重要特点是支持逻辑并发,而不是是否真正实现了物理并发。
回答by Hot Licks
The basics are simple, but the details get complex real quickly.
基础很简单,但细节很快就会变得复杂。
You can break a program into multiple threads (if it makes sense to do so), and each thread will run "at its own pace", such that if one must wait for, eg, some file I/O that doesn't slow down the others.
您可以将程序分解为多个线程(如果这样做有意义的话),并且每个线程将“按照自己的节奏”运行,这样如果必须等待,例如某些文件 I/O 不会变慢其他人。
On a single processor multiple threads are accommodated by "time slicing" the processor somehow -- either on a simple clock basis or by letting one thread run until it must wait (eg, for I/O) and then "switching" to the next thread. There is a whole art/science to doing this for maximum efficiency.
在单个处理器上,通过以某种方式对处理器进行“时间切片”来容纳多个线程——或者在一个简单的时钟基础上,或者通过让一个线程运行直到它必须等待(例如,对于 I/O)然后“切换”到下一个线。这样做是为了最大限度地提高效率,这是一门艺术/科学。
On a multi-processor (such as most modern PCs which have from 2 to 8 "cores") each thread is assigned to a separate processor, and if there are not enough processors then they are shared as in the single processor case.
在多处理器(例如大多数具有 2 到 8 个“内核”的现代 PC)上,每个线程都分配给一个单独的处理器,如果没有足够的处理器,则它们会像在单处理器情况下一样共享。
The whole area of assuring "atomicity" of operations by a single thread, and assuring that threads don't somehow interfere with each other is incredibly complex. In general a there is a "kernel" or "nucleus" category of system call that will not be interrupted by another thread, but thats only a small subset of all system calls, and you have to consult the OS documentation to know which category a particular system call falls into.
通过单个线程确保操作的“原子性”以及确保线程不会以某种方式相互干扰的整个领域非常复杂。一般来说,有一个“内核”或“核心”类别的系统调用不会被另一个线程中断,但这只是所有系统调用的一小部分,您必须查阅操作系统文档才能知道哪个类别是特定的系统调用属于。
回答by Gangadhar
Yes, A program can run two threads
at the same time.
是的,一个程序可以同时运行两个threads
。
it is called Multi threading.
它被称为 多线程。
would they both be able to run at the same time or would my second function wait until the system call finishes?
它们是否能够同时运行,还是我的第二个函数会等到系统调用完成?
They both are able to run at the same time.
它们都可以同时运行。
if you want, you can make thread B wait until Thread A completes or reverse
如果需要,您可以让线程 B 等待线程 A 完成或反转
回答by fvdalcin
They will run at the same time, for one thread is independent from another, even if you perform a system call.
它们将同时运行,因为一个线程独立于另一个线程,即使您执行系统调用。
It's pretty easy to test it though, you can create one thread that prints something to the console output and perform a system call at another thread, that you know will take some reasonable amount of time. You will notice that the messages will continue to be printed by the other thread.
不过,测试它非常容易,您可以创建一个线程将某些内容打印到控制台输出并在另一个线程上执行系统调用,您知道这将花费一些合理的时间。您会注意到消息将继续由另一个线程打印。
回答by Nitin Vats
Two thread can run concurrently only if it is running on multiple core processor system, but if it has only one core processor then two threads can not run concurrently. So only one thread run at a time and if it finishes its job then the next thread which is on queue take the time.
只有在多核处理器系统上运行时,两个线程才能并发运行,但如果只有一个核处理器,则两个线程不能并发运行。所以一次只有一个线程运行,如果它完成了它的工作,那么队列中的下一个线程需要时间。