V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
seacoastboy
V2EX  ›  Go 编程语言

多 goroutine 问题

  •  
  •   seacoastboy · 2016-03-14 12:33:08 +08:00 · 684 次点击
    这是一个创建于 2973 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package main
    import (
            "fmt"
    )
    
    func main(){
            var str = []string {"1", "2", "1","1", "2","1", "2", "1","1", "2","1", "2", "1","1", "2","1", "2", "1","1", "2", "0", "6","9","4"}
            for _, t := range str {
                    switch t {
                    case "1":
                            go go1(t)
                    case "2":
                            go go2(t)
                    default:
                            //
                    }
            }
    
            fmt.Println("hello")
    
    
    }
    
    
    func go1(st string) {
            //逻辑处理
            fmt.Println(st)
    }
    func go2(st string){
            //逻辑处理
            fmt.Println(st)
    }
    

    我开启 2 个 goroutine , 但是我不知道如何做堵塞,确保 2 个 goroutine 都运行完后在退出主 goroutine 。开始的时候我用 channel 加 计数器去实现,这样的话就需要给函数添加一个参数:

    func go1(st string, ch chan bool){
        //逻辑处理
    }
    func go2(st string, ch chan bool){
        //逻辑处理
    }
    func main(){
        //省略一些代码
        for {
            <- ch
            t ++
            if t >= len(str) {
                break
            }
        }
    }
    

    我想问问, 能不能有更简介明了的办法去实现。

    7 条回复    2016-04-27 17:13:23 +08:00
    mkeith
        1
    mkeith  
       2016-03-14 13:03:03 +08:00
    go 语言 WaitGroup 用法
    http://www.baiyuxiong.com/?p=913
    darasion
        2
    darasion  
       2016-03-14 13:03:47 +08:00
    sync 包有个 WaitGroup 可以满足你的需要。
    zts1993
        3
    zts1993  
       2016-03-14 13:13:37 +08:00
    看上去你这个好像不止会产生两个 goroutine 哦。。。
    seacoastboy
        4
    seacoastboy  
    OP
       2016-03-14 15:43:55 +08:00
    @darasion 对的使用 WaitGroup 可以实现。
    @zts1993 有 for 产生很多多 goroutine 的,

    我想使用一个全局的 channel 来实现。
    darasion
        5
    darasion  
       2016-03-24 18:28:16 +08:00
    @seacoastboy https://golang.org/doc%2Fcodewalk%2Furlpoll.go
    这里貌似有个 Poller 函数,是不是你想要的?
    想停下 goroutine 的话,发送端 close 就好。
    cobopanda
        6
    cobopanda  
       2016-04-27 16:16:42 +08:00
    看看 WaitGroup 的用法
    cobopanda
        7
    cobopanda  
       2016-04-27 17:13:23 +08:00
    以下代码仅供参考
    package main

    import (
    "fmt"
    "runtime"
    )

    func main() {
    data := []int{1, 8, 6, 7, 20}
    NumCPU := runtime.NumCPU()
    fmt.Println("NumCPU:", NumCPU)
    sem := make(chan int, NumCPU)
    for _, value := range data {
    if value%2 == 0 {
    go func(param int) {
    sem <- param
    fmt.Println("oushu:", param)
    }(value)
    } else {
    go func(param int) {
    sem <- param
    fmt.Println("jishu:", param)
    }(value)
    }
    }

    for i := 0; i < len(data); i++ {
    <-sem
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2312 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:48 · PVG 18:48 · LAX 03:48 · JFK 06:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.