官术网_书友最值得收藏!

The shared memory and shared state model

What if we write a multithreaded program to achieve the same result? A thread of execution is a sequence of programming instructions, scheduled and managed by the operating system. A process could contain multiple threads; in other words, a process is a container for concurrently executing threads, as shown in the following diagram:

As shown in the preceding diagram, multiple threads share the process memory. Two concurrently running processes do not share memory or any other resources, such as file descriptors. In other words, different concurrent processes have their own address space, while multiple threads within the same process share their address space. Each thread also has a stack of its own. This stack is used for returning after a process call. Locally scoped variables are also created on the stack. The relationships between these elements are shown in the following diagram:

As shown in the preceding diagram, both the threads communicate via the process's global memory. There is a FIFO (first in first out) queue in which the producer thread t1 enters the filenames. The consumer thread, t2, picks up the queue entries as and when it can.

What does this data structure do? It works on a similar principle as the aforementioned pipe. The producer can produce items as fast or slow as it can. Likewise, the consumer thread picks the entries from the queue as needed. Both work at their own pace, without caring or knowing of each other. 

Exchanging information this way looks simpler. However, it brings with it a host of problems. Access to the shared data structure needs to be synchronized correctly. This is surprisingly hard to achieve. The next few sections will deal with the various issues that crop up. We will also see the various paradigms that shy away from the shared state model and tilt towards message passing.

主站蜘蛛池模板: 桐城市| 刚察县| 恩平市| 西青区| 安国市| 自贡市| 呼和浩特市| 南漳县| 乐东| 云梦县| 宁强县| 遵化市| 招远市| 石河子市| 永新县| 宝兴县| 玛曲县| 松滋市| 湘西| 海盐县| 麻城市| 筠连县| 永德县| 晋宁县| 上蔡县| 麻栗坡县| 青海省| 新竹市| 北川| 保德县| 四子王旗| 阜阳市| 永定县| 呼和浩特市| 临海市| 集贤县| 双江| 左贡县| 荔浦县| 乌苏市| 平果县|