English 中文(简体)
戈兰同意如何按顺序印刷
原标题:Golang concurrency how to make function to print in an order
  • 时间:2023-12-26 18:18:29
  •  标签:
  • go

I have three functions which prints First, Second and Third. I want to print it in the order First then Second and then Third via concurrency (channels)

我正面临着按顺序排列的问题。 谁能帮助我如何实现秩序?

package main

import (
    "fmt"
    "sync"
)

type firstSecondThird struct {
    n int
}

var (
    wg     sync.WaitGroup
    first  chan bool
    second chan bool
    third  chan bool
)

func init() {
    first = make(chan bool)
    second = make(chan bool)
    // third = make(chan bool)
}

func (fn firstSecondThird) first() {
    defer wg.Done()
    for i := 0; i < fn.n; i++ {
        fmt.Print("First")
        first <- true
    }
}

func (fn firstSecondThird) second() {
    defer wg.Done()
    for i := 0; i < fn.n; i++ {
        <-first
        fmt.Print("Second")
        second <- true
    }
}

func (fn firstSecondThird) third() {
    defer wg.Done()
    for i := 0; i < fn.n; i++ {
        <-second
        fmt.Print("Third")
        // third <- true
    }
}

func main() {
    firstSecondThird := firstSecondThird{
        n: 4,
    }
    wg.Add(2)
    go firstSecondThird.first()
    go firstSecondThird.second()
    go firstSecondThird.third()
    // <-third
    wg.Wait()
}

<<>Output>:

FirstFirstSecondSecondFirstThirdThirdSecondThirdFirstSecond%
问题回答

你们正在努力利用并非易事的渠道协调处决的次序。 观察n processes:

  • for k>1, printing of k should happen after printing of k-1
  • for k=1, printing of k should happen after printing of n

因此,假体应当照此(k是功能指数):

 for i := 0; i < fn.n; i++ {
        if k==0 {
          <-ch[fn.n-1]
        } else {
          <-ch[k-1]
        }
        fmt.Print("Third")
        if k==fn.f-1 {
           ch[0]<-true
        } else {
           ch[k+1]<-true
        }
    }
package main

import (
    "fmt"
    "sync"
)

type firstSecondThird struct {
    n int
}

var (
    wg     sync.WaitGroup
    first  chan bool
    second chan bool
    third  chan bool
)

func init() {
    first = make(chan bool)
    second = make(chan bool)
    third = make(chan bool)
}

func (fn firstSecondThird) first() {
    for i := 0; i < fn.n; i++ {
        fmt.Print("First
")
        first <- true
        <-third
    }
    defer wg.Done()
}

func (fn firstSecondThird) second() {
    for i := 0; i < fn.n; i++ {
        <-first
        fmt.Print("Second
")
        second <- true
    }
    defer wg.Done()
}

func (fn firstSecondThird) third() {
    for i := 0; i < fn.n; i++ {
        <-second
        fmt.Print("Third
")
        third <- true
    }
    defer wg.Done()
}

func main() {
    firstSecondThird := firstSecondThird{
        n: 4,
    }
    wg.Add(3)
    go firstSecondThird.first()
    go firstSecondThird.second()
    go firstSecondThird.third()
    wg.Wait()
}




相关问题
minimum work size of a goroutine [closed]

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

How do you get the terminal size in Go?

How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go

What do you use to write Go [closed]

I know its a bit too early, but I ve been trying out Go (Google s Programming Language) and its kindof annoying to write code in gedit. So, my question: What do you use to experiment with Go?

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

热门标签