Skip to content
/ sync2 Public

Synchronization Primitives Beyond Go's Standard Library.

License

Notifications You must be signed in to change notification settings

mkch/sync2

Repository files navigation

sync2

Synchronization Primitives Beyond Go's Standard Library.

  1. 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
     }
  2. 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
     }
  3. 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

Dining philosophers

About

Synchronization Primitives Beyond Go's Standard Library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published