题外话:
本来预定下周一到的耳机今天(周六)中午就到了,听了一下感觉音效不错,但是也就那么回事,而且卡耳朵卡得有点紧,心想要不要退掉或者卖掉,点回去Amazon一看,已经是正常价140刀了,还是捡了个大便宜呀,可惜用老婆的号买第二个被Amazon cancel了,原因应该是两个账号用的信用卡卡号是一样的...这样理想计划买二卖一泡汤了...不要太贪心嘛
昨晚Larry请我去看电影,极品飞车,3D效果很爽,周五中午公司有披萨,结果一天没出办公室,屁股受不了,晚上本想回家的,下班太累,于是就给自己个借口去放松一下,结果错过老爸发来的Facetime,晚上11点半到家,打回去老爸问我干嘛去了,不想对老爸撒谎,如实说同事请看电影了,“咦也,你看人家”...羞愧难当
老父亲还卧病在床,不能在身边尽孝也抓紧时间做点正事吧!
接上篇,基础问题
threads and processes -- 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.
SUMMARY
Threads are easier to create than processes since they don't require a separate address space.
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.
Threads are considered lightweight because they use far less resources than processes.
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.
- A process can consist of multiple threads.