Qt-UI

Threads and Processes

Blocking and Non-Blocking | | Race Condition

A process is a program that is being executed in memory with its own memory address range.

Multiple processes can run simultaneously by assigning a time slice to each process (usually a few milliseconds). When a process has run out of time, its state is frozen and execution is passed over to next process waiting to be resumed for another time slice.

Whenever execution is resumed, the virtual address range tables of the MMU and the entire CPU need to be saved, synchronized with the IO state and reloaded. Process switching is therefore costly.

A more lightweight method to execute program code simultaneously is threading. A thread shares its virtual address range with other threads of the same process, so that switching from one thread to another just requires the reloading of the CPU registers. Most modern CPUs have multiple copies of its register set to be able to reactivate a set efficiently.

Since threads live in the same process, they share all the resources of that process. If more than a single thread accesses a resource, the access to the shared resource needs to be synchronized. Otherwise the asynchronous access will lead to a so called race condition! In a race condition the well-defined execution order is violated. As a result the program enters an ill-defined logical state, where all sorts of bad things can happen. Most common effects are a program freeze or a crash.

Blocking and Non-Blocking | | Race Condition

Options: