Is CreateDirectory() in C# thread-safe?

后端 未结 3 1533
鱼传尺愫
鱼传尺愫 2021-02-07 12:01

Can I safely attempt to create the same directory from two different threads, without having one of them throw an exception, or run into other issues?

3条回答
  •  野性不改
    2021-02-07 12:54

    To elaborate on @JaredPar's answer, you have a race condition on your hands. If the first call creates the folder completely, and only then the second call starts, everything will be OK.

    however, if the second call reaches the OS while it is still processing the first, the OS might fail the second one dune to locking issues, and you will get an exception.

    It is still thread safe in the sense that you won't get any unpredictable folders created, or no folder at all.

    To elaborate - while I'm not 100% sure that Windows doesn't have an internal race condition when the same folder is created twice concurrently, I'm pretty certain you won't be able to trash the disk by doing that, or get a deadlock with both creations being stuck to death. One of them will succeed, the other will fail, but the folder will be created.

    So your heuristics, just to be absolutely sure, should be this:

    • Create Directory
    • If it fails, wait a random amount of time (say, between 0.2 and 0.5 of a second) and try again.
    • If it fails constantly (say, 3 times in a row), you have another problem at your hands - no permissions to the folder, a full disk, etc..

      Incidentally, why not create the folder once when the application starts running?

提交回复
热议问题