Learn Data Structures and Algorithms with Golang
上QQ阅读APP看书,第一时间看更新

Synchronized queue

A synchronized queue consists of elements that need to be processed in a particular sequence. Passenger queue and ticket processing queues are types of synchronized queues, as follows:


//main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt package
import (
"fmt"
"time"
"math/rand"
)
// constants
const (
messagePassStart = iota
messageTicketStart
messagePassEnd
messageTicketEnd
)
//Queue class
type Queue struct {
waitPass int
waitTicket int
playPass bool
playTicket bool
queuePass chan int
queueTicket chan int
message chan int
}

We will discuss the different methods of synchronized queue in the following sections.