From 12855d0ea4e9225c4d1c7bc166a93dd9548f1872 Mon Sep 17 00:00:00 2001 From: Efim Zabarsky <[Efim.Zabarsky@kla-tencor.com]> Date: Mon, 26 Aug 2024 13:28:57 +0300 Subject: [PATCH] create broadcast page and link to main Readme --- README.md | 2 +- concurrency/broadcast.md | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 concurrency/broadcast.md diff --git a/README.md b/README.md index 7d9f5f7..caaeb03 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ A curated collection of idiomatic design & application patterns for Go language. |:-------:|:----------- |:------:| | [N-Barrier](/concurrency/barrier.md) | Prevents a process from proceeding until all N processes reach to the barrier | ✘ | | [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ | -| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | ✘ | +| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | ✔ | | [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ | | [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✔ | | [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ | diff --git a/concurrency/broadcast.md b/concurrency/broadcast.md new file mode 100644 index 0000000..6057330 --- /dev/null +++ b/concurrency/broadcast.md @@ -0,0 +1,49 @@ +# Broadcast Pattern + + In Go, you can create a broadcast concurrency pattern using goroutines and channels. This pattern allows you to send a message to multiple recipients simultaneously. + +## Implementation + +```go +// Broadcast sends a message to all recipients simultaneously +func Broadcast(message string, recipients ...chan string) { + var wg sync.WaitGroup + for _, recipient := range recipients { + wg.Add(1) + go func(recipient chan string) { + defer wg.Done() + recipient <- message + }(recipient) + } + wg.Wait() +} +``` + +## Usage + +```go +func main() { + // Create channels for recipients + recipient1 := make(chan string) + recipient2 := make(chan string) + recipient3 := make(chan string) + + // Start goroutines to receive messages + go func() { + fmt.Println("Recipient 1 received:", <-recipient1) + }() + go func() { + fmt.Println("Recipient 2 received:", <-recipient2) + }() + go func() { + fmt.Println("Recipient 3 received:", <-recipient3) + }() + + // Broadcast a message to all recipients + Broadcast("Hello, World!", recipient1, recipient2, recipient3) +} +``` + +1. The *Broadcast* function takes a message and a variable number of recipient channels. +1. It uses a *sync.WaitGroup* to wait for all goroutines to finish sending the message. +1. Each recipient channel receives the message simultaneously through a goroutine.