Golang Goroutine WaitGroup with Example Read it later

Rate this post

Golang is known for its concurrency and parallelism features, which allow developers to write efficient and scalable programs. One of the key components that make this possible is Goroutine, a lightweight thread of execution that runs concurrently with other Goroutines within the same address space. However, Goroutines can be challenging to manage, especially when they need to synchronize with each other. That’s where the Golang WaitGroup comes in. In this blog post, we will explore everything you need to know about Golang Goroutine WaitGroup.

The Problem with Goroutines

While Goroutines are an essential feature of Go, managing them can be challenging. When you create a Goroutine, it executes independently and concurrently with other Goroutines, and there’s no way to know when it will complete. This can be a problem, especially when you need to coordinate the execution of multiple Goroutines.

Lenskart
Give feedback powered by PlayInAd

For example, consider the following code:

func main() {
   go task1()
   go task2()
}

func task1() {
   fmt.Println("Task 1")
}

func task2() {
   fmt.Println("Task 2")
}

In this code, we create two Goroutines, task1() and task2(), using the go keyword. These Goroutines execute independently and concurrently with each other and the main() function. While this is great for concurrency, it’s challenging to know when both tasks are complete. This is where the WaitGroup comes in.

What is Golang Goroutine WaitGroup?

A WaitGroup is a built-in synchronization primitive in Golang that allows Goroutines to wait for a group of Goroutines to finish executing before continuing with their own execution. WaitGroup is particularly useful when you need to spawn multiple Goroutines that perform independent tasks and then wait for them to complete before moving on to the next step.

A WaitGroup is created using the sync package and has three methods:

  • Add(delta int): Adds delta to the WaitGroup counter. This counter is initialized to zero when the WaitGroup is created.
  • Done(): Decrements the WaitGroup counter by one. This is typically called at the end of a Goroutine.
  • Wait(): Blocks until the WaitGroup counter is zero. This is typically called at the end of the main function to wait for all Goroutines to complete.

How does Golang Goroutine WaitGroup work?

WaitGroup works by keeping track of the number of Goroutines that are currently running. You can think of it as a counter that starts at 0 and increments each time a Goroutine is spawned. When a Goroutine finishes executing, it decrements the counter. If the counter reaches 0, it means all Goroutines have completed, and the WaitGroup is released.

To use WaitGroup in your Golang code, you need to follow these steps:

Step 1: Create a WaitGroup instance

wg := sync.WaitGroup{}

Step 2: Spawn your Goroutines, and increment the WaitGroup counter

for i := 0; i < n; i++ {
    wg.Add(1)
    go func() {
        // Perform some task here
        wg.Done()
    }()
}

Step 3: Wait for all Goroutines to complete using the Wait method

wg.Wait()

In the above code, the wg.Add(1) method increments the WaitGroup counter each time a new Goroutine is spawned. The go func() keyword creates a new Goroutine that performs some task and then calls the wg.Done() method to decrement the counter when it’s finished. Finally, the wg.Wait() method blocks the main Goroutine until all other Goroutines have finished executing.

Golang Goroutine WaitGroup Best Practices

Here are some best practices to keep in mind when using WaitGroup in your Golang code:

  1. Always call wg.Add() before spawning a new Goroutine, and wg.Done() when the Goroutine has finished executing.
  2. Use defer wg.Done() to ensure that the wg.Done() method is always called, even if the Goroutine encounters an error and exits prematurely.
  3. Don’t use WaitGroup for long-running tasks or tasks that block, as this can cause deadlocks.
  4. Don’t create new WaitGroup instances unnecessarily, as this can lead to unnecessary memory allocations.
  5. Use the context.Context package to cancel Goroutines that are waiting on a WaitGroup if necessary.
Was This Article Helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *