-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathqueueMemory.go
More file actions
48 lines (46 loc) · 905 Bytes
/
queueMemory.go
File metadata and controls
48 lines (46 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package queue
import(
"errors"
"container/list"
"sync"
)
var(
_lock=new (sync.Mutex) //互斥锁
Error_QueueIsEmpty=errors.New("queue is already empty") //队列为空
)
type QueueMemory struct{
queue *list.List
//类型 0:先入先出
queueType int
}
//入队
func(this *QueueMemory) Enqueue(msg *Message) error{
if this.queue==nil{
this.queue=list.New()
}
this.queue.PushBack(msg)
return nil
}
//出队
func(this *QueueMemory) Dequeue() (*Message,error){
if this.queue==nil || this.queue.Len()==0{
return nil,Error_QueueIsEmpty
}
var msg *Message
//判断队列类型
switch(this.queueType){
case 0: //先入先出
ele:=this.queue.Front()
_lock.Lock() //修改时使用 互斥锁
msg= this.queue.Remove(ele).(*Message)
_lock.Unlock()
break
}
return msg,nil
}
func(this *QueueMemory) Count() int{
if this.queue==nil{
return 0
}
return this.queue.Len()
}