This question actually has a deeper character and a good understanding of the concepts of multithreading in general will give you an idea about this topic. In fact, there is neither a language, nor any operating system that will provide you with the ability to asynchronously interrupt a stream without warning, so as not to use them. And all these runtimes strongly advise the developer or even require the creation of multi-threaded applications based on joint or synchronous thread interruption. The reason for these common decisions and tips is that they are all built on the basis of the same common multithreaded model.
Let's compare the concepts of multiprocessing and multithreading in order to better understand the advantages and limitations of the second.
Multiprocessing involves dividing the entire runtime into many completely isolated processes controlled by the operating system. The process includes and isolates the state of the runtime environment, including the local memory of the process and the data inside it and all system resources, such as files, sockets, synchronization objects. Isolation is a critical process characteristic because it limits the spread of errors to the boundaries of the process. In other words, no process can affect the consistency of any other process in the system. The same is true for process behavior, but in a less limited and more blurry way. In such an environment, any process can be killed at any โarbitraryโ moment, because, firstly, each process is isolated, and secondly, the operating system has full knowledge of all the resources used by the process, and can release all of them without leakage , and finally, the process will not be killed by the OS at any time, but in the number of clearly defined points where the state of the process is well known.
In contrast, multithreading involves running multiple threads in a single process. But all these threads share the same unit of isolation, and there is no control over the operating system's internal state of the process. As a result, any thread can change the state of the global process, as well as ruin it. At the same time, the points at which the state of the stream is well known that it is safe to kill the stream are completely dependent on the application logic and are not known either for the operating system or for the programming language. As a result, interruption of a stream at an arbitrary moment means its destruction at an arbitrary point of its execution path and can easily lead to data loss throughout the organization, memory and handle leakage, thread leakage and locking blocks and other synchronization primitives inside the process, a closed state that prevents passage other threads.
In this regard, the general approach is to force developers to implement synchronous or joint completion of the flow, when one thread can request another completion of the flow, and another thread at a clearly defined point can check this request and begin the shutdown procedure from the well - a certain state with safe and consistent release of all global resources of the entire system and local resources of the process.
ZarathustrA Feb 19 '15 at 10:39 2015-02-19 10:39
source share