-
Notifications
You must be signed in to change notification settings - Fork 75
Open
Description
The following code works without errors in Unix and ends with error Remove of lock file failed with: remove C:\Users\i019379\go\src\testLocking\lock.lock: The process cannot access the file because it is being used by another process. in Windows.
Thus in Windows it's impossible to remove lock file if more then one process tried to make locking.
func main(){
wd, _ := os.Getwd()
lockfile := filepath.Join(wd, "lock.lock")
lock1 := flock.New(lockfile)
locked1, _ := lock1.TryLock()
if !locked1{
fmt.Println("Error - First tryLock failed")
return
}
lock2 := flock.New(lockfile)
locked2, _ := lock2.TryLock()
if locked2{
fmt.Println("Second tryLock succeeded")
return
}
err := lock2.Unlock()
if err!=nil {
fmt.Println("Unlock of second lock failed")
return
}
err = lock1.Unlock()
if err!=nil {
fmt.Println("Unlock of first lock failed")
return
}
err = os.Remove(lockfile)
if err!=nil {
fmt.Println("Remove of lock file failed with: "+err.Error())
return
}
}