Files
bulma/handoff+demo2.go
T

46 lines
869 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func testSyncHandoff() {
runtime.GOMAXPROCS(1)
var wg sync.WaitGroup
var mu sync.Mutex
// 启动G1:持有锁并阻塞
wg.Add(1)
go func() {
defer wg.Done()
mu.Lock()
defer mu.Unlock()
fmt.Println("G1: 获取锁,开始阻塞(1s")
time.Sleep(1 * time.Second) // 同步阻塞
fmt.Println("G1: 释放锁,执行完成")
}()
// 启动G2:尝试获取锁,触发Hand Off
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("G2: 尝试获取锁...")
mu.Lock()
defer mu.Unlock()
fmt.Println("G2: 获取锁,执行完成")
}()
wg.Wait()
fmt.Println("同步阻塞Hand Off验证结束")
}
func main() {
runtime.GOMAXPROCS(1)
fmt.Printf("当前P的数量: %d\n", runtime.GOMAXPROCS(0))
// testSyscallHandoff() // 注释或取消注释切换测试
testSyncHandoff()
}