5-[多线程]-线程理论

1、什么是线程

在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程
进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)
线程才是cpu上的执行单位。

  

多线程(即多个控制线程)的概念是,在一个进程中存在多个线程,多个线程共享该进程的地址空间, 

 相当于一个车间内有多条流水线,都共用一个车间的资源。例如,北京地铁与上海地铁是不同的进程,

而北京地铁里的13号线是一个线程,北京地铁所有的线路共享北京地铁所有的资源,比如所有的乘客可以被所有线路拉。

2、多线程应用举例

 

3、开启进程的两种方式:threading模块

  • multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍

扫描二维码关注公众号,回复: 38873 查看本文章

4、线程与进程的区别

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  4. New threads are easily created; new processes require duplication of the parent process.
  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.

 

5

猜你喜欢

转载自www.cnblogs.com/venicid/p/8904728.html
今日推荐