go007 go routine with channel

2 분 소요

go007 - go routine with channel

  • Indeed, Go provides channels as a safe means of communication and synchronization between goroutines. Channels serve as message queues, allowing the exchange of values between goroutines.
  • By utilizing channels, you can safely divide work among multiple goroutines. Values can be sent from a channel to each goroutine, allowing them to independently process the received values. This approach enables concurrent execution while ensuring data integrity and synchronization between goroutines.
  • Channels act as a conduit for passing data between goroutines, providing a reliable and coordinated way to share information. They facilitate the communication and coordination required for parallel or concurrent processing in Go.
package main
import (
    "fmt"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    // 1. make channel
    ch := make(chan int)

    wg.Add(1)

    // 2. create go routine
    go square(&wg, ch)
    // 4. put input in channel
    ch <- 9
    // 5. Waif fot the whole waitGroup are finished
    wg.Wait()
    // 8. main routine done
}

func square(wg *sync.WaitGroup, ch chan int) {
    // 3. there is nothing in channel
    // so waiting for it
    n := <- ch

    // 6. get number from channel
    // and run these code
    time.Sleep(time.Second)
    fmt.Printf("Square: %d\n", n*n)
    // 7. this wait group were done
    wg.Done()
}

deadlock

  • By default, channels in Go do not have any buffer, which means that sending a value on a channel will block until another goroutine is ready to receive the value. If there are no goroutines actively attempting to receive from the channel, and the channel has no buffer to temporarily store the value, the main routine or the sending routine will wait indefinitely for a receiver to become available. This situation leads to a deadlock.
  • To avoid such deadlocks, it is important to ensure that there is a corresponding receiver for every value being sent on a channel, or utilize buffering in the channel to allow for temporary storage. Deadlocks can be prevented by carefully coordinating the sending and receiving operations on channels, ensuring that both sides are ready and available to proceed.
package main
import (
    "fmt"
)

func main() {
    // 1. make channel with buffer 0
    ch := make(chan int)

    // 2. put value to channel
    ch <- 9
    // channel doesn't have buffer at all
    // main routine stopped and waiting for forever
    // So, deadlock
    fmt.Println("== no go routine takes parameter from channel")
}

댓글남기기