首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 15:18:32 +08:00
commit d6393266b0
193 changed files with 14287 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package main
import (
"fmt"
"sync"
)
var (
sharedCounter int // 共享资源
mu sync.Mutex // 互斥锁
wg sync.WaitGroup
)
// 对共享计数器进行累加
func muIncrement() {
defer wg.Done()
for i := 0; i < 1000; i++ {
mu.Lock() // 加锁:进入临界区
sharedCounter++
mu.Unlock() // 解锁:退出临界区
}
}
func main() {
wg.Add(5) // 启动5个goroutine并发修改
for i := 0; i < 5; i++ {
go muIncrement()
}
wg.Wait() // 等待所有goroutine完成
fmt.Printf("最终计数器值: %d(预期:5000\n", sharedCounter)
}