码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-04-22 10:53:59      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:comm   height   created   exe   不同   processes   hat   ima   control   

1、什么是线程

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

  技术分享图片

技术分享图片

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

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

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

2、多线程应用举例

技术分享图片

 

技术分享图片

 

 技术分享图片

 

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

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

技术分享图片技术分享图片

 

 

技术分享图片技术分享图片

技术分享图片

 

 

 

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

 

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

标签:comm   height   created   exe   不同   processes   hat   ima   control   

原文地址:https://www.cnblogs.com/venicid/p/8904728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!