Name some advantages of Goroutines over threads

Technology CommunityCategory: GolangName some advantages of Goroutines over threads
VietMX Staff asked 3 years ago
  • Goroutines are extremely cheap to create when compared to threads. They are only a few kb in stack size and the stack can grow and shrink according to needs of the application whereas in the case of threads the stack size has to be specified and is fixed.
  • The Goroutines are multiplexed to fewer number of OS threads. There might be only one thread in a program with thousands of Goroutines. If any Goroutine in that thread blocks say waiting for user input, then another OS thread is created and the remaining Goroutines are moved to the new OS thread.
  • Goroutines communicate using channels. Channels by design prevent race conditions (a race condition occurs when two or more threads can access shared data and they try to change it at the same time) from happening when accessing shared memory using Goroutines. Channels can be thought of as a pipe using which Goroutines communicate.