package main import ( "fmt" "os" "sync" "syscall" "time" ) //文件锁 type FileLock struct { dir string f *os.File } func New(dir string) *FileLock { return &FileLock{ dir: dir, } } //加锁 func (l *FileLock) Lock() error { f, err := os.Open(l.dir) if err != nil { return err } l.f = f err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) if err != nil { return fmt.Errorf("cannot flock directory %s - %s", l.dir, err) } return nil } //释放锁 func (l *FileLock) Unlock() error { defer l.f.Close() return syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN) } func main() { test_file_path, _ := os.Getwd() locked_file := test_file_path wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go func(num int) { flock := New(locked_file) err := flock.Lock() if err != nil { wg.Done() fmt.Println(err.Error()) return } fmt.Printf("output : %d\n", num) wg.Done() }(i) } wg.Wait() time.Sleep(2 * time.Second) }在 Windows 系统下运行上面的代码会出现下面的错误:
undefined: syscall.Flock
undefined: syscall.LOCK_EX
undefined: syscall.LOCK_NB
undefined: syscall.Flock
undefined: syscall.LOCK_UN
syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
我们采用了 syscall.LOCK_EX、syscall.LOCK_NB,这是什么意思呢?
本文链接:http://task.lmcjl.com/news/14505.html