// Homework 39, challenge: A pool with a progress line. // // Twelve jobs, four workers. Job n takes n times 100 milliseconds. While the // workers run, main prints a progress line every 500 milliseconds, such as // "progress: 5 of 12 done". When every job is done, it prints "All 12 jobs // done." Ctrl+C stops everything: the workers stop, even in the middle of a // job, and main prints "Stopped by Ctrl+C after N of 12 jobs." // // In main, one select waits for a result or a tick. After Ctrl+C, the workers // stop and results is closed, so main finds out from the results channel. // // Run with: go run exercises/39/homework/04-progress.go package main import ( "fmt" "time" ) const jobCount = 12 // TODO: write doJob(ctx, n), which waits n*100ms or until ctx is done, and a // worker(ctx, jobs, results) that uses it. func main() { // TODO: make the context with signal.NotifyContext, start four workers, // send the jobs, and close results when the workers are done. Then loop // with select until every result is in or ctx is done. start := time.Now() fmt.Println("TODO: run", jobCount, "jobs") fmt.Println("Took about", time.Since(start).Round(100*time.Millisecond)) }