How can I achieve mutual exclusion in concurrent goroutines using WaitGroup?

Linda Hamilton
Release: 2024-11-01 13:57:02
Original
207 people have browsed it

How can I achieve mutual exclusion in concurrent goroutines using WaitGroup?

Mutual Exclusion of Concurrent Goroutines Using WaitGroup

In your code, you have three concurrent goroutines that need to execute without interference from each other during specific sections of their code. This concept is known as mutual exclusion, and it ensures that only one goroutine can execute a critical section of code at a time.

To achieve mutual exclusion using WaitGroup, you can follow these steps:

  1. Create a Mutex for Each Concurrent Goroutine: A mutex is a locking mechanism that allows only one goroutine to acquire a lock at a time. Create a separate mutex for each goroutine that needs to execute its critical section exclusively.
  2. Acquire the Mutex Before Entering the Critical Section: Before executing the critical section of code in each goroutine, call the Lock() method on the corresponding mutex. This operation will block the goroutine until the mutex is acquired.
  3. Release the Mutex After Leaving the Critical Section: After completing execution of the critical section, release the mutex by calling the Unlock() method. This allows other goroutines to acquire the mutex and enter their critical sections.
  4. Use WaitGroup to Wait for Completion: Create a WaitGroup to track the completion of all goroutines. Each goroutine should call the Done() method of the WaitGroup after completing its execution. The main goroutine should wait for all goroutines to complete using the Wait() method of the WaitGroup.

Here's an example that implements the above steps:

<code class="go">package main

import (
    "fmt"
    "sync"
)

var (
    mutex1 sync.Mutex
    mutex2 sync.Mutex
    mutex3 sync.Mutex
    wg     sync.WaitGroup
)

func Routine1() {
    mutex1.Lock()
    defer mutex1.Unlock()

    // Do something
    for i := 0; i < 200; i++ {
        mutex2.Lock()
        mutex3.Lock()
        fmt.Println("value of z")
        mutex2.Unlock()
        mutex3.Unlock()
    }
    // Do something
}

func Routine2() {
    mutex2.Lock()
    defer mutex2.Unlock()

    // Do something
    for i := 0; i < 200; i++ {
        mutex1.Lock()
        mutex3.Lock()
        fmt.Println("value of z")
        mutex1.Unlock()
        mutex3.Unlock()
    }
    // Do something
}

func Routine3() {
    mutex3.Lock()
    defer mutex3.Unlock()

    // Do something
    for i := 0; i < 200; i++ {
        mutex1.Lock()
        mutex2.Lock()
        fmt.Println("value of z")
        mutex1.Unlock()
        mutex2.Unlock()
    }
    // Do something
}

func main() {
    wg.Add(3)
    go Routine1()
    go Routine2()
    go Routine3()
    wg.Wait()
}</code>
Copy after login

In this example, the critical section of each goroutine is the loop where it executes fmt.Println("value of z"). The mutexes ensure that only one goroutine can execute this section at a time. The WaitGroup ensures that the main goroutine waits for all goroutines to complete before exiting.

The above is the detailed content of How can I achieve mutual exclusion in concurrent goroutines using WaitGroup?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!