reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-14 5459ba1d3f7f944aa97923ed9c09a5dbc7663928
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package common
 
import (
    "container/list"
    "sync"
)
 
// LockList list
type LockList struct {
    cache *list.List
    cv    *sync.Cond
    cond  bool
    size  int
}
 
// NewLockList new
func NewLockList(size int) *LockList {
    return &LockList{
        cache: list.New(),
        cv:    sync.NewCond(&sync.Mutex{}),
        cond:  false,
        size:  size,
    }
}
 
// Push push
func (l *LockList) Push(v interface{}) {
    l.cv.L.Lock()
    l.cache.PushBack(v)
 
    for l.cache.Len() > l.size {
        l.cache.Remove(l.cache.Front())
    }
 
    l.cond = true
    l.cv.Signal()
    l.cv.L.Unlock()
}
 
// Pop pop
func (l *LockList) Pop() []interface{} {
 
    var batch []interface{}
 
    l.cv.L.Lock()
 
    for !l.cond {
        l.cv.Wait()
    }
 
    elem := l.cache.Front()
    if elem != nil {
        batch = append(batch, elem.Value)
        l.cache.Remove(l.cache.Front())
    }
 
    l.cond = false
    l.cv.L.Unlock()
 
    return batch
}
 
// Drain drain all element
func (l *LockList) Drain() []interface{} {
 
    var batch []interface{}
 
    l.cv.L.Lock()
 
    for !l.cond {
        l.cv.Wait()
    }
 
    for {
 
        elem := l.cache.Front()
        if elem == nil {
            break
        }
 
        batch = append(batch, elem.Value)
        l.cache.Remove(l.cache.Front())
    }
 
    l.cond = false
    l.cv.L.Unlock()
 
    return batch
}