初级会员
  • 第 9283 位会员
  • marlonche
  • 2017-06-25 02:46:22
  • Offline
  • 40 90

最近发布的主题

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • 显示出来的内容和原始内容不一样,麻烦管理员看看
  • #1 @marlonche ```go package main import ( "fmt" "math/rand" "math" ) var arr []int var arrChan []chan chan int var exp int = 12 //sort 2**exp integers using exp goroutines func main() { initArr(int(math.Pow(2, float64(exp)))) for i:=1; i < exp; i++ { go work(i) } boot(0) result := <- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i < num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i < exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i < len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch min := i max := i+1 if arr[min] > arr[max] { max = i min = i+1 } ch <- arr[min] ch <- arr[max] close(ch) } } func work(index int) { //fmt.Println("work:", index) in := arrChan[index] for { ch1, ok := <- in if !ok { close(arrChan[index+1]) break } ch2 := <- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = <- ch1 v2 = <- ch2 if v1 < v2 { v1min = true } } else { if v1min { v1, ok = <-ch1 if !ok { v1closed = true v1min = false } else if !v2closed && v1 > v2 { v1min = false } } else { v2, ok = <- ch2 if !ok { v2closed = true v1min = true } else if !v1closed && v1 < v2 { v1min = true } } } if v1closed && v2closed { close(ch) break } if v1min { ch <- v1 } else { ch <- v2 } } } } ```
  • package main import ( "fmt" "math/rand" "math" ) var arr []int var arrChan []chan chan int var exp int = 12 //sort 2**exp integers using exp goroutines func main() { initArr(int(math.Pow(2, float64(exp)))) for i:=1; i < exp; i++ { go work(i) } boot(0) result := <- arrChan[len(arrChan)-1] for i := range result { fmt.Println(i) } } func initArr(num int) { arr = make([]int, num) for i:=0; i < num; i++ { arr[i]=rand.Intn(10000) } arrChan = make([]chan chan int, exp+1) for i:=0; i < exp+1; i++{ arrChan[i] = make(chan chan int, 2) } } func boot(index int) { for i := 0; i < len(arr); i += 2 { ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch min := i max := i+1 if arr[min] > arr[max] { max = i min = i+1 } ch <- arr[min] ch <- arr[max] close(ch) } } func work(index int) { //fmt.Println("work:", index) in := arrChan[index] for { ch1, ok := <- in if !ok { close(arrChan[index+1]) break } ch2 := <- in ch := make(chan int, int64(math.Pow(2, float64(index+1)))) arrChan[index+1] <- ch var v1, v2 int var v1min, init, v1closed, v2closed bool for { if !init { init = true v1 = <- ch1 v2 = <- ch2 if v1 < v2 { v1min = true } } else { if v1min { v1, ok = <-ch1 if !ok { v1closed = true v1min = false } else if !v2closed && v1 > v2 { v1min = false } } else { v2, ok = <- ch2 if !ok { v2closed = true v1min = true } else if !v1closed && v1 < v2 { v1min = true } } } if v1closed && v2closed { close(ch) break } if v1min { ch <- v1 } else { ch <- v2 } } } }
  • 当data race存在时,程序运行的行为是不确定的,所以这样的代码没有实际意义
  • 和c++里面的非虚函数处理一样,在编译的时候就通过类的类型确定了要调用的方法