-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.go
56 lines (48 loc) · 1.3 KB
/
channel.go
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
package common
//===----------------------------------------------------------------------===//
//
// 🚄 ElenaDB ®
//
// channel.go
//
// Identification: pkg/common/channel.go
//
// Copyright (c) 2024
//
//===----------------------------------------------------------------------===//
/*
Channels allow for safe sharing of data between threads. This is a multi-producer multi-consumer channel.
*/
import (
"sync"
)
type Channel[T any] struct {
cond *sync.Cond // condition variable para manejar la sincronización
// FLAG_ESTRUCTURA: queue
queue []T // almacena los elementos
}
func NewChannel[T any]() *Channel[T] {
ch := &Channel[T]{
queue: make([]T, 0),
}
ch.cond = sync.NewCond(&sync.Mutex{})
return ch
}
// inserta elemento en la cola.
func (ch *Channel[T]) Put(element T) {
ch.cond.L.Lock()
defer ch.cond.L.Unlock()
ch.queue = append(ch.queue, element)
ch.cond.Broadcast()
}
// extrae un elemento de la cola de manera segura.
func (ch *Channel[T]) Get() T {
ch.cond.L.Lock()
defer ch.cond.L.Unlock()
for len(ch.queue) == 0 {
ch.cond.Wait() // si la cola está vacía, el hilo se bloquea y espera hasta que un nuevo elemento esté disponible. utiliza la condition variable para esta sincronización.
}
element := ch.queue[0]
ch.queue = ch.queue[1:]
return element
}