refer to http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/

A process is an executing instance of an application. What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word. A thread is a path of execution within a process. Also, a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.

It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

MultiThreading

Threads, of course, allow for multi-threading. A common example of the advantage of multithreading is the fact that you can have a word processor that prints a document using a background thread, but at the same time another thread is running that accepts user input, so that you can type up a new document.

If we were dealing with an application that uses only one thread, then the application would only be able to do one thing at a time – so printing and responding to user input at the same time would not be possible in a single threaded application.

Each process has it’s own address space, but the threads within the same process share that address space. Threads also share any other resources within that process. This means that it’s very easy to share data amongst threads, but it’s also easy for the threads to step on each other, which can lead to bad things.

Multithreaded programs must be carefully programmed to prevent those bad things from happening. Sections of code that modify data structures shared by multiple threads are called critical sections. When a critical section is running in one thread it’s extremely important that no other thread be allowed into that critical section. This is called synchronization, which we wont get into any further over here. But, the point is that multithreading requires careful programming.

Also, context switching between threads is generally less expensive than in processes. And finally, the overhead (the cost of communication) between threads is very low relative to processes.

Here’s a summary of the differences between threads and processes:

1. Threads are easier to create than processes since they
don't require a separate address space. 2. Multithreading requires careful programming since threads
share data strucures that should only be modified by one thread
at a time. Unlike threads, processes don't share the same
address space. 3. Threads are considered lightweight because they use far
less resources than processes. 4. Processes are independent of each other. Threads, since they
share the same address space are interdependent, so caution
must be taken so that different threads don't step on each other.
This is really another way of stating #2 above. 5. A process can consist of multiple threads.

我的总结:

1. Threads are used for small ‘light weight’ tasks, whereas processes are used for more ‘heavyweight’ tasks.

2. Threads within the same process share the same address space, allow reading writing from the same data structure, whereas processes do not. They use IPC, and is very resource intensive.

thread如何安全访问共享内存->mutex,semaphone/mutex区别

Mutex:

Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "Mutexes are typically used to serialise access to a section of  re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."

最新文章

  1. 解决Wireshark没有网卡问题
  2. 服务器配置ssl证书支持苹果ATS方法
  3. 解决HierarchyViewer不能连接真机的问题
  4. Java类初始化顺序问题
  5. NAS4Free 配置BT下载
  6. wampserver 自定义站点
  7. 把WCF服务部署服务器IIS异常(详细:处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”)
  8. Git学习之路(2)-安装GIt和创建版本库
  9. Javascript中的Microtask和Macrotask——从一道很少有人能答对的题目说起
  10. iOS开发 -------- Block技术中的weak - strong
  11. 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性
  12. python第四天 三级菜单新思路
  13. PAT L2-016 愿天下有情人都是失散多年的兄妹(深搜)
  14. 【2019北京集训2】duck 线段树优化建图+tarjan
  15. rem手机端适配
  16. 浅谈Laravel框架的CSRF
  17. 怎样安装TortoiseGit
  18. win10下搭建jz2440v3(arm s3c2440)开发及gdb调试环境【转】
  19. 高德地图 Android编程中 如何设置使 标记 marker 能够被拖拽
  20. flask第十七篇——模板【1】

热门文章

  1. T-SQL 操作练习
  2. 如何获取DIV的id
  3. 字典型转换为JSON数据
  4. JS位操作符
  5. Java学习-031-JSON 之五 -- 特定数据获取(JSONObject满足特定键值)
  6. [代码片段]读取BMP文件(二)
  7. http://blog.csdn.net/littlechang/article/details/8642149
  8. NSUserDefaults 可以保存哪些类型
  9. php自定义错误处理和try{}catch(){}学习
  10. Spring第十篇—举例实现AOP