[Mono-list] Threading and IO problem

Yanko Hernández Álvarez yhdezalvarez at gmail.com
Thu Mar 6 12:50:17 EST 2008


I've made a small program to use all processors on a system to analyze N
text files. But when it's run on a 8 CPU PC, the CPU time stays around
70%-80% and the idle time between 20-30%.

I  made a small program to illustrate the problem, it was simplified it to
the point it doesn't process anything. It just creates a thread for every
processor on the system and every thread just reads an entire file passed as
parameter:
------------------------------------
using System;
using System.IO;
using System.Threading;

namespace MultipleReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread[] Threads = new Thread[Environment.ProcessorCount];
            for (int Idx = 0; Idx < Threads.Length; Idx++)
            {
                MyThread T = new MyThread(args[0]);
                Threads[Idx] = new Thread(new ThreadStart(T.Process));
            }
            foreach (Thread T in Threads)
                T.Start();
            foreach (Thread T in Threads)
                T.Join();
        }
    }

    class MyThread
    {
        private string FileName;

        public MyThread(string FileName)
        {
            this.FileName = FileName;
        }

        public void Process()
        {
            string Str;
            using (StreamReader SR = new StreamReader(FileName))
                while ((Str = SR.ReadLine()) != null) ;
        }
    }
}
------------------------------------
When this program is run using a large text file as a parameter (a ~600 megs
file, to make the reading last in the order of tens of seconds) the same
behavior is observed. Is this normal? What is causing this behavior? Is
there any way to use all the processors at full capacity?

The file is fully cached (cat file /dev/null) on RAM (4G RAM on this PC)
before the program is started.

    Yanko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080306/7237a2c6/attachment.html 


More information about the Mono-list mailing list