简体   繁体   English

File.ReadAllText线程安全吗?

[英]Is File.ReadAllText thread safe?

Specifically will spawning a thread using the TPL Task.Factory.StartNew : 具体来说,将使用TPL Task.Factory.StartNew一个线程:

Task.Factory.StartNew(() => {
       File.ReadAllText(@"thisFile.txt");
});

Causing any issues, etc? 引起任何问题等? There doesn't appear to be any mention of thread safety on the MSDN resource MSDN资源上似乎没有提到线程安全性

It's in a SOAP web service environment. 它位于SOAP Web服务环境中。

Ps Please, I don't want to know about the pro's and cons of using a Task in a web environment. 请注意,我不想知道在网络环境中使用任务的专业和缺点。 I'm fully aware of these issues, please , just take it for granted that in my case this model is justified, thanks. 我完全了解这些问题, 理所当然地认为在我的情况下这个模型是合理的,谢谢。

It's fine - assuming nothing's writing to the file at the same time, in which case you may not be able to open the file (or might see partial writes). 没关系 - 假设没有任何内容同时写入文件,在这种情况下,您可能无法打开文件(或者可能看不到部分写入)。

As per the documentation of File : 根据文件的File

Any public static (Shared in Visual Basic) members of this type are thread safe. 此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的。 Any instance members are not guaranteed to be thread safe. 任何实例成员都不保证是线程安全的。

(Not that there can be any instance methods, as it's a static class...) (不是说可以有任何实例方法,因为它是一个静态类......)

Yes, that will be thread-safe in itself; 是的,这本身就是线程安全的; however, it is still subject to the usual rules of the file-system: concurrent access to the same file depends on which flags were used by the competing handles. 但是,它仍然遵循文件系统的通常规则:对同一文件的并发访问取决于竞争句柄使用的标志。 If any handle has it marked for exclusive access, then it will fail with an IO-related exception. 如果任何句柄标记为独占访问,则它将失败并出现与IO相关的异常。

There is actually no such thing as "thread safe" without defining what operations are used. 实际上没有“线程安全”这样的东西,没有定义使用什么操作。

If all the threads (and processes!) are just reading the file in either way, the read is safe. 如果所有线程(和进程!)只是以任何一种方式读取文件,那么读取是安全的。 If however some of the threads (or another processes) is writing into the file, you might get a half-up-to-date information, you never know how the writing is organized. 但是,如果某些线程(或其他进程)正在写入文件,那么您可能会得到一份最新的信息,您永远不会知道如何组织写入。

For a more fail-proof access, you could use 要获得更多防故障访问,您可以使用

using (var s = new FileStream(..., FileMode.Open, FileAccess.Read, FileShare.None))
using (var tr = new StreamReader(s))
{
    content = tr.ReadToEnd();
}

The documentation for File.ReadAllText doesn't state anything and therefore doesn't guarantee anything about locking. File.ReadAllText文档没有说明任何内容,因此不保证锁定的任何内容。

Here is a way similar to what Vlad proposed. 这是一种类似于弗拉德提出的方法。 I'm using the FileShare option of Read, so other streams can read from the same file. 我正在使用Read的FileShare选项,因此其他流可以从同一个文件中读取。

byte[] buffer;
using (FileStream fs = new FileStream("pathGoesHere", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    buffer = new byte[fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM