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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!