Why wouldn’t multi-threading speed help

I had some code

foreach(String f in FilesToProcess) ProcessFile(f, opts); 

It was taking about 3-10 seconds per file to process but was taking too long. So, I thought… “Hey! What about multi-threading?” So, I tried this…

 Thread[] threadPool = new Thread[opts.Threads]; while(FilesToProcess.Count()>0) { for(int i=0;i<opts.Threads;i++) { if (threadPool[i]!=null && !threadPool[i].IsAlive) { threadPool[i].Join(); threadPool[i] = null; } if (threadPool[i]==null && FilesToProcess.Count>0) { threadPool[i] = new Thread(new ParameterizedThreadStart(ProcessFile)); ProcessFileThreadArgs targs = new ProcessFileThreadArgs(FilesToProcess.Dequeue(), opts); threadPool[i].Start(targs); } } } foreach(Thread t in threadPool) { if (t != null) t.Join(); } 

But, the process has less throughput now.

submitted by /u/eightvo
[link] [comments]

Leave a Reply