A very old question that I know, but hopefully someone finds this interesting.
I also got confusing results using PowerShell to create a shared folder for use as a mutex, so I created a test script.
function New-FolderMutex { try { New-Item -ItemType directory -Path .\TheMutex -ErrorAction Stop > $null $true } catch { $false } } function Remove-FolderMutex { Remove-Item -Path .\TheMutex } 1..100 | % { if (New-FolderMutex) { Write-Host "Inside loop $_" Remove-FolderMutex } }
This script is executed while the current directory is in the network share.
When I ran this script simultaneously in two separate PowerShell consoles, it was clear from the error messages that the approach was doomed. There are a number of different errors caused by the Remove-Item call, although it is only called by the process that created this folder. It seems like a whole bunch of non-nuclear steps are going on behind the scenes.
Of course, the OP asked about mkdir (possibly a system call), and my example uses PowerShell cmdlets with a much higher level, but I hope this is of some interest.
An example of the output of one of two processes (edited for brevity)
Inside loop 30 Inside loop 31 Inside loop 32 Inside loop 33 Inside loop 34 Remove-Item : Access to the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex' is denied. At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5 + Remove-Item -Path .\TheMutex + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], UnauthorizedAccessException + FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand Inside loop 39 Remove-Item : H:\My Documents\PowerShell\MutexFolder\TheMutex is a NTFS junction point. Use the Force parameter to delete or modify. At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5 + Remove-Item -Path .\TheMutex + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (H:\My Documents...Folder\TheMutex:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand Inside loop 42 Remove-Item : Could not find a part of the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex'. At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5 + Remove-Item -Path .\TheMutex + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], DirectoryNotFoundException + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand Inside loop 44 Inside loop 45
source share