同时运行两个线程 [英] Running two threads at the same time

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

问题描述

我想知道一个程序是否可以同时运行两个线程(这基本上就是正确使用的线程吗?).但是,如果我要在运行在线程A上的一个函数中进行系统调用,而又要在运行在线程B上的另一个函数中运行其他任务,那么它们都可以同时运行还是我第二个可以运行?功能等到系统调用完成?

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?

我的原始问题的附加:现在,在进行系统调用时,此过程是否仍将是不间断的过程?我说的是在UNIX/LINUX上使用任何系统调用.

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.

推荐答案

多线程和并行处理是两个完全不同的主题,每个主题都有其自己的特色,但是为了介绍起见...

Multi-threading and parallel processing are two completely different topics, each worthy of its own conversation, but for the sake of introduction...

线程:
当您启动可执行文件时,它正在进程中的线程中运行.当您启动另一个线程时,将其称为线程2,您现在在同一进程中有2个单独运行的执行链(线程).在单核微处理器(uP) 上,运行多个线程,但不能并行运行.尽管从概念上讲线程通常被称为在同一时间运行,但实际上它们实际上是在操作系统分配和控制的时间片中连续运行的.这些切片彼此交错.因此,线程1的执行步骤实际上不会与线程2的执行步骤同时发生.这些行为通常会扩展到您创建的多个线程,即,执行链的数据包都在同一进程中工作并共享时间.切片由操作系统分发出去.

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.

因此,在您的系统调用示例中,在允许另一个线程的执行步骤继续执行之前,它实际上取决于系统调用是否完成.有几种因素会影响到发生的情况:这是阻塞呼叫吗?一个线程是否比另一个线程具有更高的优先级.时间片的持续时间是多少?

So, in your system call example, 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?

与C中线程相关的链接:
SO示例
POSIX
ANSI C

Links relevant to threading in C:
SO Example
POSIX
ANSI C

并行处理:
当在多核系统(多个uP或多个多核uP)上执行多线程程序时,线程可以同时运行,也可以并行运行,因为不同的线程可能会拆分为单独的内核以共享工作量.这是并行处理的一个示例.

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 parallel as different threads may be split off to separate cores to share the workload. This is one example of parallel processing.

同样,从概念上讲,并行处理和线程被认为是相似的,因为它们允许事情同时完成.但这仅仅是概念,它们在目标应用程序和技术上确实有很大的不同.在这种情况下,线程可以用作识别和拆分进程中整个任务的一种方式(例如,TCP/IP服务器可以在请求新的连接时启动工作线程,然后进行连接并保持该连接,只要保持连接状态即可) ),并行处理通常用于将"相同任务"的较小部分(例如,可以在不同位置独立执行的一组复杂的计算)发送给各个资源(核心或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 genetics and MMORPG gaming.

与C中的并行处理有关的链接:
OpenMP
更多OpenMP (示例)
NVIDIA的CUDA Tookit

Links relevant to parallel processing in C:
OpenMP
More OpenMP (examples)
Gribble Labs - Introduction to OpenMP
CUDA Tookit from NVIDIA

有关线程和体系结构一般主题的其他阅读:

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.

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

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