doushouj966020 2013-12-23 13:31
浏览 87
已采纳

Go中的同时生产者和消费者

I want to create a producer/consumer with manager program in Go. For example: I have a 5 producers, 5 consumers and manager. Producers have their own local arrays, they iterate over them and send the elements to the manager. Consumers have their own local arrays with info that elements consume; they send them to the manager too. The Manager has it own array, where it stores what and how many elements there are (for example - if the producer sends 1 1 2 3 1 2 0 elements, the manager array looks like 1 3 2 1 (one 0, three 1, two 2 and one 3 ) and it handles producers' and consumers' requests - placing an element into the array (produce) or removing it (consume).

Is it possible to make a program like this in Go? I already did this in JAVA + CSP with channels to send info and guards in the manager to determine which procedure should be done first when producer and consumer try to process the same element (for example, a producer wants to add 1 to the manager array and at the same time a consumer wants to consume 1).

Any examples or advice are welcome, because I don't find any info about what I want to do. If needed I can give my JAVA+CSP code.

UPDATE. How about synchronization (not to take from empty array)? For example - if consumer wants to consume element from manager array that does not exist yet (for example consumer wants to consume '3', but manager don't have any of thems) but producer has this element and it will be produced after few iterations - how can I make consumers to check manager array again and again until producers work is finished? Should I need to create structs (or classes) for consumers elements and mark that they are used or not, or Go has specific methods to do this?

  • 写回答

2条回答 默认 最新

  • dongyuan4790 2013-12-23 16:40
    关注

    Here's a full example, including channel cleanup. After all the consumers and producers are finished, the Manager prints out the result (for which I've used a map rather than a slice, since I think it makes the code a tad easier).

    package main
    
    import "fmt"
    
    // Consume processes the numbers in ns, sending them on ch after they're
    // processed. When the routine is finished, it signals that on done.
    func Consume(done chan bool, ch chan int, ns []int) {
        for i := range ns {
            ch <- i
        }
        done <- true
    }
    
    // Produce "creates" the numbers in ns, sending them on ch after they're
    // produced. When the routine is finished, it signals that on done.
    func Produce(done chan bool, ch chan int, ns []int) {
        for i := range ns {
            ch <- i
        }
        done <- true
    }
    
    // Manage creates consumers and producers for the given int slices.
    // It returns once all consumers and producers are finished.
    func Manage(cons, pros [][]int) {
        cch := make(chan int)
        pch := make(chan int)
        dch := make(chan bool)
        n := len(cons) + len(pros)
        data := make(map[int]int)
        for _, c := range cons {
            go Consume(dch, cch, c)
        }
        for _, p := range pros {
            go Produce(dch, pch, p)
        }
        for n > 0 {
            select {
            case c := <-cch:
                data[c] -= 1
            case c := <-pch:
                data[c] += 1
            case <-dch:
                n -= 1
            }
        }
        close(cch)
        close(pch)
        close(dch)
        fmt.Println(data)
    }
    func main() {
        cons := [][]int{{1, 3, 5}, {0, 1, 5}}
        pros := [][]int{{0, 1, 1}, {3, 5, 5, 7}}
        Manage(cons, pros)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog