Parallel Programming I/O Help

Long story short, I have a collection of parallel work tasks that all end by creating and writing to these a file. The method I use to generate filenames checks for name collisions and changes the name accordingly until no collision exists (e.g. "file (1).ext" if "file.ext" already exists). The problem I have is that since I'm running these tasks in parallel, often a thread, A, will create a file in the space between another thread, B, checking for a filename collision then writing the file, resulting the thread B failing because thread A has already created that named file. Is there any interlocked method of 'claiming' a filename that is threadsafe and not prone to race conditions?

by Sidneys1 via /r/csharp

Leave a Reply