Concurrency Theory - LogeshVel/go_programs GitHub Wiki

image

Not all tasks can be parallelized

image

image

Concurrent

image

image

image

image

Process

image

OS

image

image

Scheduling Processes

image

Context Switching

image

Threads

image

Switching between the threads in a process is much faster than switching the Process. Since the Process has large context(state) to store and switch between Process by the OS

Goroutines

image

The OS will start the main thread, the switching between the Goroutines is handled by the Go Runtime scheduler

image

Interleaving

Leaving the execution of one task and jumps to execute next task. We never know at which point(instruction) the switch happens.

image

image

Race Condition

Simply, when multiple threads tries to access a resource at the same time the race condition occurs. Technically not same time since Concurrent executes one at a time, race condition occurs when one thread works with one data and before completing that work with that data the next thread started (context switching happened) and the new thread tries to modify/work with that data, again context switch happens now the old task continues its work but the shared data is altered/modified. This condition is race condition

image

image

image

image

image