关于go的调度问题

jingyugao · · 215 次点击 · 开始浏览    置顶
按照go的说明,goroutine的调度方式为先运行最后一个goroutinue,剩下的按照顺序依次运行。 若无系统调用则不发生调度 若无非内敛函数调用,则不发生抢占式调度(go 1.4) 可是以下代码的结果不太明白 // main func main() { data := make([]byte, 256) for i := 0; i < 256; i++ { data[i] = byte(i) } runtime.GOMAXPROCS(1) exitChan := make(chan int, 10) t0 := time.Now().UnixNano() for i := 0; i < 10; i++ { go func(j int) { ret := uint64(0) for x := uint64(0); x < uint64(j*40000000); x++ { ret = uint64(utils.MurmurHash2(data, uint32(ret))) } exitChan <- j }(i) } for i := 0; i < 10; i++ { fmt.Print(<-exitChan, time.Now().UnixNano()-t0, "\n") } } // Mixing constants; generated offline. const ( M = 0x5bd1e995 BIGM = 0xc6a4a7935bd1e995 R = 24 BIGR = 47 ) // 32-bit mixing function. func mix2(h uint32, k uint32) (uint32, uint32) { k *= M k ^= k >> R k *= M h *= M h ^= k return h, k } // The original MurmurHash2 32-bit algorithm by Austin Appleby. func MurmurHash2(data []byte, seed uint32) (h uint32) { var k uint32 // Initialize the hash to a 'random' value h = seed ^ uint32(len(data)) // Mix 4 bytes at a time into the hash for l := len(data); l >= 4; l -= 4 { k = uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | uint32(data[3])<<24 h, k = mix2(h, k) data = data[4:] } // Handle the last few bytes of the input array switch len(data) { case 3: h ^= uint32(data[2]) << 16 fallthrough case 2: h ^= uint32(data[1]) << 8 fallthrough case 1: h ^= uint32(data[0]) h *= M } // Do a few final mixes of the hash to ensure the last few bytes are well incorporated h ^= h >> 13 h *= M h ^= h >> 15 return }
215 次点击  
加入收藏 微博
6 回复  |  直到 2018-09-14 17:55:49
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传