简体   繁体   English

C#:FileStream.ReadByte() 是一个多线程友好的函数吗?

[英]C#: is FileStream.ReadByte() a multi-threading friendly function?

So I have 16 threads that simultaneously run this method:所以我有 16 个线程同时运行这个方法:

    private void Work()
    {
        int currentByte;
        char currentChar;

        try
        {
            while (true)
            {
                position++;
                currentByte = file.ReadByte();
                currentChar = Convert.ToChar(currentByte);

                entries.Add(new Entry(currentChar));
            }
        }
        catch (Exception) { }
    }

And then I have one more thread running this method:然后我还有一个线程运行这个方法:

    private void ManageThreads()
    {
        bool done;

        for(; ; )
        {
            done = !threads.Any(x => x.IsAlive == true);//Check if each thread is dead before continuing
            if (done)
                break;
            else
                Thread.Sleep(100);
        }
        PrintData();
    }

Here is the problem: the PrintData method just prints everything in the 'entries' list to a text file.问题在于:PrintData 方法只是将“条目”列表中的所有内容打印到文本文件中。 This text file is different every time the program is run even with the same input file.即使使用相同的输入文件,每次运行程序时,该文本文件也是不同的。 I am a bit of a noob when it comes to multi-threaded applications so feel free to dish out the criticism.当谈到多线程应用程序时,我有点菜鸟,所以请随时提出批评。

In general unless type explicitly calls out thread safety in its documentation you should assume it is not thread-safe * .一般来说,除非 type 在其文档中明确指出线程安全,否则您应该假设它不是线程安全的* Streams in .Net do not have such section and should be treated non-thread safe - use appropriate synchronization (ie locks) that guarantees that each stream is accessed from one thread at a time. .Net 中的流没有这样的部分,应该被视为非线程安全的 - 使用适当的同步(即锁)来保证每次从一个线程访问每个流。

With file streams there is another concern - OS level file object may be updated from other threads - FileStream tries to mitigate it by checking if its internal state matches OS state - see FileStream:remarks section on MSDN.对于文件流,还有另一个问题——操作系统级别的文件对象可能会从其他线程更新FileStream试图通过检查其内部状态是否与操作系统状态匹配来缓解它——请参阅 MSDN 上的FileStream:remarks部分。

If you want thread safe stream you can try to use Synchronized method as shown in C#, is there such a thing as a "thread-safe" stream?如果您想要线程安全的流,您可以尝试使用C# 中所示的Synchronized方法,是否有“线程安全”流之类的东西? . .

Note that code you have in the post will produce random results whether stream is thread safe or not.请注意,无论流是否线程安全,您在帖子中的代码都会产生随机结果。 Thread safety of a stream will only guarantee that all bytes show up in output.流的线程安全只会保证所有字节都显示在输出中。 If using non thread safe stream there is no guarantees at all and some bytes may show up multiple times, some skipped and any other behavior (crashes, partial reads,...) are possible.如果使用非线程安全流,则根本无法保证,并且某些字节可能会出现多次,有些会被跳过,并且任何其他行为(崩溃、部分读取……)都是可能的。


* Thread-safe as in "internal state of the instance will be consistent whether it is called from one thread or multiple". *线程安全,如“无论是从一个线程还是多个线程调用,实例的内部状态都将保持一致”。 It does not mean calling arbitrary methods from different threads will lead to useful behavior.并不意味着从不同线程调用任意方法会导致有用的行为。

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

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