完善gchan和gqueue注释

This commit is contained in:
John
2018-02-07 09:42:18 +08:00
parent 68a66128e6
commit 5db721a250
3 changed files with 32 additions and 26 deletions

View File

@ -4,13 +4,13 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 优雅的channel操作封装
// 优雅的channel操作.
package gchan
import (
"sync"
"sync/atomic"
"errors"
"sync/atomic"
)
type Chan struct {
@ -28,7 +28,7 @@ func New(limit int) *Chan {
// 将数据压入队列
func (q *Chan) Push(v interface{}) error {
if atomic.LoadInt32(&q.closed) > 0 {
return errors.New("channel closed")
return errors.New("closed")
}
q.list <- v
return nil

View File

@ -5,14 +5,17 @@
// You can obtain one at https://gitee.com/johng/gf.
// 动态大小的安全队列(dynamic channel).
// 优点:
// 1、队列初始化速度快
// 2、可以向队头/队尾进行Push/Pop操作
package gqueue
import (
"math"
"sync"
"container/list"
"sync/atomic"
"errors"
"sync/atomic"
"container/list"
)
type Queue struct {
@ -40,7 +43,7 @@ func New(limit...int) *Queue {
// 将数据压入队列, 队尾
func (q *Queue) PushBack(v interface{}) error {
if atomic.LoadInt32(&q.closed) > 0 {
return errors.New("queue closed")
return errors.New("closed")
}
if q.limit > 0 {
q.limits <- struct{}{}
@ -55,7 +58,7 @@ func (q *Queue) PushBack(v interface{}) error {
// 将数据压入队列, 队头
func (q *Queue) PushFront(v interface{}) error {
if atomic.LoadInt32(&q.closed) > 0 {
return errors.New("queue closed")
return errors.New("closed")
}
if q.limit > 0 {
q.limits <- struct{}{}
@ -104,7 +107,7 @@ func (q *Queue) PopBack() interface{} {
return nil
}
// 关闭队列(通知所有通过Pop阻塞的协程退出)
// 关闭队列(通知所有通过Pop*阻塞的协程退出)
func (q *Queue) Close() {
if atomic.LoadInt32(&q.closed) == 0 {
atomic.StoreInt32(&q.closed, 1)

View File

@ -1,12 +1,15 @@
package main
import (
"time"
)
func main() {
defer func() {
recover()
}()
events1 := make(chan int, 100)
events2 := make(chan int, 100)
events1 <- 1
close(events1)
events1 <- 2
//events2 := make(chan int, 100)
//go func() {
// for{
// v := <- events1
@ -15,18 +18,18 @@ func main() {
//
//}()
go func() {
time.Sleep(2*time.Second)
events1 <- 1
events2 <- 2
time.Sleep(2*time.Second)
close(events1)
close(events2)
events1 <- 1
events2 <- 2
}()
select {
}
//go func() {
// time.Sleep(2*time.Second)
// events1 <- 1
// events2 <- 2
// time.Sleep(2*time.Second)
// close(events1)
// close(events2)
// events1 <- 1
// events2 <- 2
//}()
//
//select {
//
//}
}