package main

import (
	"fmt"
	"sync"
	"time"
)

const N = 10 // 任务数量

func main() {
	var wg sync.WaitGroup

	for i := 1; i <= N; i++ {
		wg.Add(1) // 计数器 +1
		go func(id int) {
			defer wg.Done() // 任务完成时 -1
			fmt.Printf("task %d start\n", id)
			time.Sleep(time.Duration(id*100) * time.Millisecond) // 模拟耗时
			fmt.Printf("task %d done\n", id)
		}(i)
	}

	fmt.Println("main: waiting for all tasks...")
	wg.Wait() // 阻塞直到计数器为 0
	fmt.Println("main: all tasks completed, exit")
}
