Go 标准库并没有提供多线程(pthread)的接口。但 Go Runtime Scheduler 在调度 goroutine 时,是可能创建多个线程的,比如 goroutine 1 在执行 blocking I/O 时,go 必须创建新的线程来执行其他的 goroutine。
因此,在多个 goroutine 中访问共享的资源时,一样要考虑同步(synchronization)的问题。
Go 的 sync 包 提供了一些常见的设施:
pthread_once()
同时参考 pthread 的 同步机制。