标签:int cli mil c++ 类型 ack cond void struct
go代码中要实现异步很简单,go funcName()。
但是进程需要控制协程数量在合理范围内,对应大批量任务可以使用“协程池 + 无锁队列”实现。
package main
import (
"fmt"
"time"
)
type DataContainer struct {
Queue chan interface{}
}
func NewDataContainer(max_queue_len int) (dc *DataContainer){
dc = &DataContainer{}
dc.Queue = make(chan interface{}, max_queue_len)
return dc
}
//非阻塞push
func (dc *DataContainer) Push(data interface{}, waittime time.Duration) bool{
click := time.After(waittime)
select {
case dc.Queue <- data:
return true
case <- click:
return false
}
}
//非阻塞pop
func (dc *DataContainer) Pop(waittime time.Duration) (data interface{}){
click := time.After(waittime)
select {
case data =<-dc.Queue:
return data
case <- click:
return nil
}
}
//test
var MAX_WAIT_TIME = 10 *time.Millisecond
func main(){
type dataItem struct {
name string
age int
}
datacotainer := NewDataContainer(2)
//add
fmt.Printf("res=%v\n", datacotainer.Push(&dataItem{"zhangsan",25}, MAX_WAIT_TIME))
fmt.Printf("res=%v\n", datacotainer.Push(&dataItem{"lisi",30}, MAX_WAIT_TIME))
fmt.Printf("res=%v\n", datacotainer.Push(&dataItem{"wangwu",28}, MAX_WAIT_TIME))
//get
var item interface{}
item = datacotainer.Pop(MAX_WAIT_TIME)
if item != nil{
if tmp,ok := item.(*dataItem); ok{ //interface转为具体类型
fmt.Printf("item name:%v, age:%v\n", tmp.name, tmp.age)
}
}
}
Go语言无锁队列组件的实现 (chan/interface/select)
标签:int cli mil c++ 类型 ack cond void struct
原文地址:https://www.cnblogs.com/xudong-bupt/p/10199028.html