forked from steger/pr3-sose2026
66 lines
3.1 KiB
Go
66 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
/**
|
|
* This function reads integers from a receive-only channel and prints them to the console.
|
|
* It simulates some work by sleeping for 100 milliseconds after printing each integer.
|
|
* The function uses a done channel to signal when it has finished processing all integers from the input channel.
|
|
* When the input channel is closed and all integers have been printed, the function sends a value to the done channel to indicate that it is done.
|
|
*
|
|
* @param ci A receive-only channel of integers that the function will read from and print. The function will continue to read from the channel until it is closed.
|
|
* @param done A send-only channel of booleans that the function will use to signal when it has finished processing the input channel. The function will send a value to this channel when it is done.
|
|
*/
|
|
func printChannel(ci <-chan int, done chan<- bool) {
|
|
defer func() { done <- true }()
|
|
for i := range ci {
|
|
time.Sleep(100 * time.Millisecond)
|
|
fmt.Println(i)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This function is similar to printChannel but uses a WaitGroup instead of a done channel to signal completion.
|
|
* The WaitGroup allows us to wait for the goroutine to finish without needing a separate channel for signaling.
|
|
*
|
|
* @param ci A receive-only channel of integers that the function will read from and print. The function will continue to read from the channel until it is closed.
|
|
* @param wg A pointer to a sync.WaitGroup that will be used to signal when the goroutine has finished processing the channel.
|
|
* The WaitGroup should have its counter incremented before calling this function, and this function will call Done() on the WaitGroup when it finishes.
|
|
*/
|
|
func printWaitgroup(ci <-chan int, wg *sync.WaitGroup) {
|
|
defer func() { wg.Done() }()
|
|
for i := range ci {
|
|
time.Sleep(100 * time.Millisecond)
|
|
fmt.Println(i)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
|
|
c := make(chan int) //no capacity, unbuffered channel. This means that sends to the channel will block until another goroutine is ready to receive from the channel, and receives from the channel will block until another goroutine sends a value to the channel.
|
|
// This is useful for synchronizing between goroutines, as it ensures that the sender and receiver are coordinated.
|
|
|
|
// Use a channel named done to signal when the printChannel goroutine has finished processing the input channel.
|
|
// This allows us to wait for the goroutine to complete before exiting the main function.
|
|
//done := make(chan bool)
|
|
//go printChannel(c, done)
|
|
|
|
// Use a WaitGroup to wait for the printWaitgroup goroutine to finish instead of using a done channel.
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
go printWaitgroup(c, &wg)
|
|
|
|
c <- 1
|
|
c <- 2
|
|
c <- 3
|
|
close(c) // Close the channel to signal to the printChannel goroutine that there are no more values to read.
|
|
// This will cause the for loop in printChannel to exit and the function to send a value to the done channel, allowing the main function to wait for it to finish.
|
|
|
|
//<-done //alternative to using a done channel, we can wait for the WaitGroup to finish instead.
|
|
wg.Wait() // Wait for the printWaitgroup goroutine to finish before exiting the main function.
|
|
}
|