Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created February 24, 2015 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/f586c73b55525db09a15 to your computer and use it in GitHub Desktop.
Save mdwhatcott/f586c73b55525db09a15 to your computer and use it in GitHub Desktop.
Don't ever do this (but it works).
package main
import (
"fmt"
"strings"
"sync"
)
func main() {
var waiter sync.WaitGroup
waiter.Add(10)
go exhaust(&waiter)
waiter.Wait()
fmt.Printf("Hackity-hack!!!")
}
// exhaust drains the counter of the provided *sync.WaitGroup to avoid a deadlock.
// <hack>
// It uses the reflection-based capability of the fmt package to observe the
// internal state of the provided sync.WaitGroup. Who says you can't access
// unexported fields?
// </hack>
func exhaust(waiter *sync.WaitGroup) {
for {
state := fmt.Sprintf("%#v", waiter)
if strings.Contains(state, "counter:0") {
break
} else {
waiter.Done()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment