- Concurrent Patterns and Best Practices
- Atul S. Khot
- 332字
- 2021-07-16 17:32:29
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.
- Linux操作系統(tǒng)基礎(chǔ)
- Designing Purpose:Built Drones for Ardupilot Pixhawk 2.1
- Linux系統(tǒng)架構(gòu)與運(yùn)維實(shí)戰(zhàn)
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)踐教程(Windows 7+Office 2013)
- 循序漸進(jìn)學(xué)Docker
- Microsoft Operations Management Suite Cookbook
- OpenSolaris設(shè)備驅(qū)動(dòng)原理與開(kāi)發(fā)
- Kali Linux高級(jí)滲透測(cè)試(原書(shū)第3版)
- 鴻蒙HarmonyOS手機(jī)應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)
- Linux系統(tǒng)安全:縱深防御、安全掃描與入侵檢測(cè)
- 基于Arduino的嵌入式系統(tǒng)入門與實(shí)踐
- 微軟360度
- Microsoft DirectAccess Best Practices and Troubleshooting
- Windows 11使用方法與技巧從入門到精通
- 鴻蒙應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)