shop

package
v0.0.0-...-38a5715 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package shop implements the sleeping barber problem. There is one barber in the barber shop, one barber chair and n chairs for waiting customers. If there are no customers, the barber sits down in the barber chair and takes a nap. An arriving customer must wake the barber. Subsequent arriving customers take a waiting chair if any are empty or leave if all chairs are full.

Have the ability to close the shop even if new customers are entering. Customers looking for a chair should run on their own goroutine.

Task: Change EnterCustomer so a customer can wait for a specified amount of time for a chair to open up.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrShopClosed is returned when the shop is closed.
	ErrShopClosed = errors.New("shop closed")

	// ErrNoChair is returned when all the chairs are occupied.
	ErrNoChair = errors.New("no chair available")
)

Functions

This section is empty.

Types

type Shop

type Shop struct {
	// contains filtered or unexported fields
}

Shop represents the barber's shop which contains chairs for customers that customers can occupy and the barber can service. The shop can be closed for business.

func Open

func Open(maxChairs int) *Shop

Open creates a new shop for business and gets the barber working.

func (*Shop) Close

func (s *Shop) Close()

Close prevents any new customers from entering the shop and waits for the barber to finish all existing customers.

func (*Shop) EnterCustomer

func (s *Shop) EnterCustomer(name string) error

EnterCustomer is called to create a customer to be serviced. If the shop is closed the function returns an error. If the shop is open, a goroutine is created to handle the customers concurrently.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL