Skip to content

Instantly share code, notes, and snippets.

@griimick
Forked from nitishfy/mp_mc.go
Created July 10, 2024 20:59
Show Gist options
  • Select an option

  • Save griimick/d2623b92b0d28f6aefdcf79b487c97cf to your computer and use it in GitHub Desktop.

Select an option

Save griimick/d2623b92b0d28f6aefdcf79b487c97cf to your computer and use it in GitHub Desktop.

Revisions

  1. @nitishfy nitishfy revised this gist Jul 10, 2024. 4 changed files with 21 additions and 15 deletions.
    14 changes: 9 additions & 5 deletions multiP_multiC.go → mp_mc.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // multiple producers, multiple consumers
    package main

    import (
    @@ -24,15 +25,18 @@ func consumers(id int, ch <-chan int, wg *sync.WaitGroup) {

    func main() {
    ch := make(chan int)
    var prowg sync.WaitGroup
    var conswg sync.WaitGroup
    var prowg sync.WaitGroup // separate waitgroup for producers
    var conswg sync.WaitGroup // separate waitgroup for consumers

    numProducers := 2
    numConsumers := 3

    for i:=1; i <= 2; i++ {
    for i:=1; i <= numProducers; i++ {
    prowg.Add(1)
    go producers(i, ch, &prowg)
    }

    for i:=1; i <= 3; i++ {
    for i:=1; i <= numConsumers; i++ {
    conswg.Add(1)
    go consumers(i, ch, &conswg)
    }
    @@ -43,4 +47,4 @@ func main() {
    }()

    conswg.Wait()
    }
    }
    14 changes: 7 additions & 7 deletions multiP_singleC.go → mp_sc.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // multiple producers, single consumer
    package main

    import (
    @@ -25,9 +26,10 @@ func consumers(id int, ch <-chan int, wg *sync.WaitGroup) {
    func main() {
    ch := make(chan int)
    var wg sync.WaitGroup
    var prowg sync.WaitGroup

    for i:=1; i <= 2; i++ {
    var prowg sync.WaitGroup // separate waitgroup for producers
    numProducers := 2

    for i:=1; i <= numProducers; i++ {
    prowg.Add(1)
    go producers(i, ch, &wg)
    }
    @@ -37,10 +39,8 @@ func main() {
    close(ch)
    }()

    for i:=1; i <= 1; i++ {
    wg.Add(1)
    go consumers(i, ch, &wg)
    }
    wg.Add(1)
    go consumers(i, ch, &wg)

    wg.Wait()
    }
    1 change: 1 addition & 0 deletions singleP_multiC.go → sp_mc.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    //single producer, multiple consumers
    package main

    import (
    7 changes: 4 additions & 3 deletions singleP_singleC.go → sp_sc.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // single producer, single consumer
    package main

    import (
    @@ -9,15 +10,15 @@ func producer(ch chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for i := 1; i <= 100; i++ {
    ch <- i
    fmt.Println("Produced:", i)
    fmt.Printf("goroutine produce the value:%d\n", i)
    }
    close(ch)
    }

    func consumer(ch <-chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    for num := range ch {
    fmt.Println("Consumed:", num)
    for val := range ch {
    fmt.Printf("goroutine consumed the value:%d\n", val)
    }
    }

  2. @nitishfy nitishfy created this gist Jul 10, 2024.
    46 changes: 46 additions & 0 deletions multiP_multiC.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    package main

    import (
    "fmt"
    "sync"
    )

    // producers sends the value to the channel
    func producers(id int, ch chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for i:=1; i <= 100; i++ {
    fmt.Printf("goroutine %d sent the value %d\n", id, i)
    ch <- i
    }
    }

    // consumers consumes the values
    func consumers(id int, ch <-chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    for val := range ch {
    fmt.Printf("value received by goroutine %d: %d\n",id, val)
    }
    }

    func main() {
    ch := make(chan int)
    var prowg sync.WaitGroup
    var conswg sync.WaitGroup

    for i:=1; i <= 2; i++ {
    prowg.Add(1)
    go producers(i, ch, &prowg)
    }

    for i:=1; i <= 3; i++ {
    conswg.Add(1)
    go consumers(i, ch, &conswg)
    }

    go func () {
    prowg.Wait()
    close(ch)
    }()

    conswg.Wait()
    }
    46 changes: 46 additions & 0 deletions multiP_singleC.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    package main

    import (
    "fmt"
    "sync"
    )

    // producers sends the value to the channel
    func producers(id int, ch chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for i:=1; i <= 100; i++ {
    fmt.Printf("goroutine %d sent the value %d\n", id, i)
    ch <- i
    }
    }

    // consumers consumes the values
    func consumers(id int, ch <-chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    for val := range ch {
    fmt.Printf("value received by goroutine %d: %d\n",id, val)
    }
    }

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

    for i:=1; i <= 2; i++ {
    prowg.Add(1)
    go producers(i, ch, &wg)
    }

    go func () {
    prowg.Wait()
    close(ch)
    }()

    for i:=1; i <= 1; i++ {
    wg.Add(1)
    go consumers(i, ch, &wg)
    }

    wg.Wait()
    }
    38 changes: 38 additions & 0 deletions singleP_multiC.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package main

    import (
    "fmt"
    "sync"
    )

    func producer(ch chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for i := 1; i <= 100; i++ {
    ch <- i
    fmt.Println("Produced:", i)
    }
    close(ch)
    }

    func consumer(id int, ch <-chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    for num := range ch {
    fmt.Printf("Consumer %d consumed: %d\n", id, num)
    }
    }

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

    wg.Add(1)
    go producer(ch, &wg)

    numConsumers := 3
    for i := 1; i <= numConsumers; i++ {
    wg.Add(1)
    go consumer(i, ch, &wg)
    }

    wg.Wait()
    }
    35 changes: 35 additions & 0 deletions singleP_singleC.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    package main

    import (
    "fmt"
    "sync"
    )

    func producer(ch chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for i := 1; i <= 100; i++ {
    ch <- i
    fmt.Println("Produced:", i)
    }
    close(ch)
    }

    func consumer(ch <-chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    for num := range ch {
    fmt.Println("Consumed:", num)
    }
    }

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

    wg.Add(1)
    go producer(ch, &wg)

    wg.Add(1)
    go consumer(ch, &wg)

    wg.Wait()
    }