Dan Willemsen | bc60c3c | 2021-12-15 01:09:00 -0800 | [diff] [blame] | 1 | // Copyright 2021 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package a |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | type WrapperWithLock[T any] interface { |
| 13 | PrintWithLock() |
| 14 | } |
| 15 | |
| 16 | func NewWrapperWithLock[T any](value T) WrapperWithLock[T] { |
| 17 | return &wrapperWithLock[T]{ |
| 18 | Object: value, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | type wrapperWithLock[T any] struct { |
| 23 | Lock sync.Mutex |
| 24 | Object T |
| 25 | } |
| 26 | |
| 27 | func (w *wrapperWithLock[T]) PrintWithLock() { |
| 28 | w.Lock.Lock() |
| 29 | defer w.Lock.Unlock() |
| 30 | |
| 31 | fmt.Println(w.Object) |
| 32 | } |