I'm using concurrent dictionary to hold open files.
To open a new file, I do this:
myDictionary.GetOrAdd (fName, (fn) => new StreamWriter(fn, true));
And with this, I regularly get following exception:
System.IO.IOException: The process cannot access the file '....' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPa
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
To me that means that the operation is not atomic and that the software is trying to open the same file twice.
Is there any other operation I can use instead, which will be atomic? I want to avoid locking because of performance considerations.
Since, as Ameen points out, the guarantee is that the key/value pair will only be added once, you can hold a ConcurrentDictionary<string, Lazy<StreamWriter>>
, and the value factory should construct the Lazy<StreamWriter>
by passing the LazyThreadSafetyMode.ExecutionAndPublication
argument (second argument, after the instantiation delegate).
This way, you get to limit the lock per-file, without locking the entire dictionary.
private static ConcurrentDictionary<string, Lazy<StreamWriter>> _myDictionary =
new ConcurrentDictionary<string, Lazy<StreamWriter>>();
public static StreamWriter GetOrCreate(string fName)
{
return _myDictionary.GetOrAdd(fName,
new Lazy<StreamWriter>(() => new StreamWriter(fName),
LazyThreadSafetyMode.ExecutionAndPublication)).Value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With