什么是线程安全_什么是线程?

什么是线程安全

什么是线程? (What are Threads?)

Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads are also known as Lightweight processes. Threads are popular way to improve application through parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel.

线程是一个执行单元,由其自己的程序计数器,堆栈和一组寄存器组成。 线程也称为轻量级进程。 线程是通过并行改进应用程序的流行方法。 CPU在线程之间来回快速切换,给人以线程并行运行的错觉。

As each thread has its own independent resource for process execution, multpile processes can be executed parallely by increasing number of threads.

由于每个线程都有自己的独立资源来执行进程,因此可以通过增加线程数来并行执行多进程。

什么是线程安全_什么是线程?

螺纹类型 (Types of Thread)

There are two types of threads:

有两种类型的线程:

  1. User Threads

    用户线程
  2. Kernel Threads

    内核线程

User threads, are above the kernel and without kernel support. These are the threads that application programmers use in their programs.

用户线程位于内核之上,没有内核支持。 这些是应用程序程序员在其程序中使用的线程。

Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously.

操作系统本身的内核内支持内核线程 。 所有现代操作系统都支持内核级线程,从而允许内核执行多个同时任务和/或同时服务多个内核系统调用。

多线程模型 (Multithreading Models)

The user threads must be mapped to kernel threads, by one of the following strategies:

用户线程必须通过以下策略之一映射到内核线程:

  • Many to One Model

    多对一模型
  • One to One Model

    一对一模型
  • Many to Many Model

    多对多模型

多对一模型 (Many to One Model)

  • many to one model, many user-level threads are all mapped onto a single kernel thread.多对一模型中,许多用户级线程都映射到单个内核线程上。
  • Thread management is handled by the thread library in user space, which is efficient in nature.

    线程管理由用户空间中的线程库处理,这实际上是有效的。

什么是线程安全_什么是线程?

一对一模型 (One to One Model)

  • one to one model creates a separate kernel thread to handle each and every user thread.一对一模型创建一个单独的内核线程来处理每个用户线程。
  • Most implementations of this model place a limit on how many threads can be created.

    该模型的大多数实现都限制了可以创建多少个线程。
  • Linux and Windows from 95 to XP implement the one-to-one model for threads.

    从95到XP的Linux和Windows实现线程的一对一模型。

什么是线程安全_什么是线程?

多对多模型 (Many to Many Model)

  • many to many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models.多对多模型将任意数量的用户线程多路复用到相等或更少数量的内核线程上,并结合了一对一和多对一模型的最佳功能。
  • Users can create any number of the threads.

    用户可以创建任意数量的线程。
  • Blocking the kernel system calls does not block the entire process.

    阻止内核系统调用不会阻止整个过程。
  • Processes can be split across multiple processors.

    进程可以拆分为多个处理器。

什么是线程安全_什么是线程?

什么是线程库? (What are Thread Libraries?)

Thread libraries provide programmers with API for creation and management of threads.

线程库为程序员提供了用于创建和管理线程的API。

Thread libraries may be implemented either in user space or in kernel space. The user space involves API functions implemented solely within the user space, with no kernel support. The kernel space involves system calls, and requires a kernel with thread library support.

线程库可以在用户空间或内核空间中实现。 用户空间涉及仅在用户空间内实现的API函数,没有内核支持。 内核空间涉及系统调用,并且需要具有线程库支持的内核。

三种螺纹 (Three types of Thread)

  1. POSIX Pitheads, may be provided as either a user or kernel library, as an extension to the POSIX standard.POSIX Pitheads可以作为用户或内核库提供,作为POSIX标准的扩展。
  2. Win32 threads, are provided as a kernel-level library on Windows systems.Win32线程作为Windows系统上的内核级库提供。
  3. Java threads: Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pitheads or Win32 threads depending on the system.Java线程 :由于Java通常在Java虚拟机上运行,​​因此线程的实现取决于JVM在其上运行的任何操作系统和硬件,即Pitheads或Win32线程,具体取决于系统。

多线程的好处 (Benefits of Multithreading)

  1. Responsiveness

    React性
  2. Resource sharing, hence allowing better utilization of resources.

    资源共享,因此可以更好地利用资源。
  3. Economy. Creating and managing threads becomes easier.

    经济。 创建和管理线程变得更加容易。
  4. Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a series of processors to scale.

    可扩展性。 一个线程在一个CPU上运行。 在多线程进程中,线程可以分布在一系列处理器上以进行扩展。
  5. Context Switching is smooth. Context switching refers to the procedure followed by CPU to change from one task to another.

    上下文切换很流畅。 上下文切换是指CPU从一个任务更改为另一个任务所遵循的过程。

多线程问题 (Multithreading Issues)

Below we have mentioned a few issues related to multithreading. Well, it's an old saying, All good things, come at a price.

下面我们提到了一些与多线程有关的问题。 好吧,这是句老话, 所有的好东西都是有代价的。

线程取消 (Thread Cancellation)

Thread cancellation means terminating a thread before it has finished working. There can be two approaches for this, one is Asynchronous cancellation, which terminates the target thread immediately. The other is Deferred cancellation allows the target thread to periodically check if it should be cancelled.

线程取消意味着在线程完成工作之前将其终止。 可以有两种方法,一种是异步取消 ,该方法立即终止目标线程。 另一个是“ 延迟取消”,它允许目标线程定期检查是否应取消它。

信号处理 (Signal Handling)

Signals are used in UNIX systems to notify a process that a particular event has occurred. Now in when a Multithreaded process receives a signal, to which thread it must be delivered? It can be delivered to all, or a single thread.

UNIX系统中使用信号来通知进程发生了特定事件。 现在,当多线程进程接收到信号时,必须将其传递到哪个线程? 它可以传递给所有线程,也可以传递给单个线程。

fork()系统调用 (fork() System Call)

fork() is a system call executed in the kernel through which a process creates a copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process be copied or not?

fork()是在内核中执行的系统调用,进程通过该调用创建自身的副本。 现在,多线程进程中的问题是,如果有一个线程分叉,是否会复制整个进程?

安全问题 (Security Issues)

Yes, there can be security issues because of extensive sharing of resources between multiple threads.

是的,由于多个线程之间广泛的资源共享,因此可能存在安全问题。

There are many other issues that you might face in a multithreaded process, but there are appropriate solutions available for them. Pointing out some issues here was just to study both sides of the coin.

在多线程过程中,可能还会遇到许多其他问题,但是有适合他们的解决方案。 在这里指出一些问题只是为了研究硬币的两面。

翻译自: https://www.studytonight.com/operating-system/multithreading

什么是线程安全