Sharing resources with process or threads

The OS ensures that there is little or no interaction between processes. When creating a multiprocessing application, where multiple processes must interact, a common OS resource must be explicitly shared. This can be a common file, a specific, shared-memory object, or a semaphore with a shared state between the processes. Processes are inherently independent; interaction among them is exceptional.

Multiple threads, in contrast, are part of a single process; all threads of a process share OS resources. We can make an exception to get some thread-local memory that can be freely written without interference from other threads. Outside thread-local memory, operations that write to memory can set the internal state of the process in a potentially unpredictable order. Explicit locking must be used to avoid problems. As noted previously, the overall sequence of instruction executions is rarely, strictly speaking, concurrent. The instructions from concurrent threads and processes are generally interleaved among the cores in an unpredictable order. With threading comes the possibility of destructive updates to shared variables and the need for mutually exclusive access through locking.

At the bare metal hardware level, there are some complex memory write situations. For more information on issues with memory writes, visit http://en.wikipedia.org/wiki/Memory_disambiguation

The existence of concurrent object updates can create havoc when trying to design multithreaded applications. Locking is one way to avoid concurrent writes to shared objects. Avoiding shared objects in general is another viable design technique. The second technique—avoiding writes to shared objects—is more applicable to functional programming.

In CPython, the GIL is used to ensure that OS thread scheduling will not interfere with the internals of maintaining Python data structures. In effect, the GIL changes the granularity of scheduling from machine instructions to groups of Python virtual machine operations.

The impact of the GIL in term of ensuring data structure integrity is often negligible. The greatest impact on performance comes from the choice of algorithm. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset