Synchronization Primitives Beyond Go's Standard Library.
-
Cancellable Sleep:
func ExampleSleep() { ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond) defer cancel() // It won't sleep for an hour fmt.Println(sync2.Sleep(ctx, time.Hour)) // Output: // context deadline exceeded }
-
Cancellable mutex:
func ExampleMutexGroup_Cancel() { ctx, cancel := context.WithCancel(context.Background()) g := sync2.NewMutexGroupWithContext(ctx) m := g.NewMutex() m.Lock() go cancel() fmt.Println(m.Lock()) // Won't block for long. // Output: // context canceled }
-
Atomic locking of multiple mutexes:
func ExampleMutexGroup_LockAll() { g := sync2.NewMutexGroup() m1, m2 := g.NewMutex(), g.NewMutex() m1.Lock() m2.Lock() go func() { m1.Unlock() fmt.Println("m1 unlocked") m2.Unlock() fmt.Println("m2 unlocked") }() g.LockAll(m1, m2) fmt.Println("atomically locked m1 and m2") // Output: // m1 unlocked // m2 unlocked // atomically locked m1 and m2 }
The full example is available here: Dining philosophers problem