Home  >  Article  >  Backend Development  >  Concurrency patterns in Go: CSP and message passing

Concurrency patterns in Go: CSP and message passing

WBOY
WBOYOriginal
2024-06-02 13:08:57406browse

When programming concurrently in Go, it is crucial to understand and use appropriate patterns. CSP is a concurrency mode based on sequential processes, implemented using Goroutine, and is suitable for simple communication. Message passing is a pattern that uses channels as message queues for communication, and is suitable for complex or multiple Goroutine interaction scenarios. In practical applications, CSP can be used to implement simple message services, sending and receiving messages between different Goroutines through channels.

Concurrency patterns in Go: CSP and message passing

Concurrency Patterns in Go: CSP and Message Passing

When programming concurrently in Go, understand and use appropriate Patterns matter. Communicating Sequential Processes (CSP) and message passing are two common concurrency patterns in Go that provide different ways to efficiently coordinate concurrent operations.

CSP

CSP is a concurrent mode based on sequential processes that alternately perform send and receive operations. Go has built-in Goroutines, making the CSP pattern a simple and powerful option.

package main

import "fmt"

func main() {
    ch := make(chan int)
    go func() {
        ch <- 42
    }()
    fmt.Println(<-ch)
}

In this example, a Goroutine is responsible for sending an integer to channel ch, and the main Goroutine receives the integer from the channel.

Messaging

Messaging is another pattern used for communication between concurrent processes. Channels in Go are essentially message queues that allow Goroutines to exchange data safely and efficiently.

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    ch := make(chan string)
    wg.Add(1)
    go func() {
        defer wg.Done()
        ch <- "Hello"
    }()
    wg.Wait()
    fmt.Println(<-ch)
}

In this example, an additional sync.WaitGroup is used to synchronize the execution of different Goroutines. Goroutine sends string messages to channel ch, and main Goroutine receives messages from this channel.

When to use each mode

Choosing to use CSP or messaging depends on the needs of your application:

  • Use CSP for simplicity communication, where a single Goroutine sends and receives data.
  • Use message passing for more complex communication, where multiple Goroutines interact with a shared communication medium (i.e. channel).

Practical

A practical example is to use CSP to implement a simple messaging service:

package main

import (
    "fmt"
    "sync"
)

type Message struct {
    From, To, Content string
}

func main() {
    ch := make(chan Message)
    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        defer wg.Done()
        senderMessages(ch)
    }()

    go func() {
        defer wg.Done()
        receiveMessages(ch)
    }()

    wg.Wait()
}

func senderMessages(ch chan Message) {
    ch <- Message{From: "John", To: "Jane", Content: "Hello"}
    ch <- Message{From: "Jane", To: "John", Content: "Hi"}
}

func receiveMessages(ch chan Message) {
    for msg := range ch {
        fmt.Println(msg)
    }
}

This example demonstrates how to use CSP mode sends and receives messages between different Goroutines.

The above is the detailed content of Concurrency patterns in Go: CSP and message passing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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