开发者

Is it possible to split looping through a file into many threads?

I am trying to implement a solution I got to another question I asked (http://stackoverflow.com/questions/7166223/create-password-breaker-for-iphone-backup-files ). The solution is very straight forward. The problem is that I am using a dictionary with about 250 000 words in it. For every word I add letters and numbers in certain patterns, to get the different combinations I normally use. I have taken away the combinations I rarely use, but still have about 24 combinations for every word, hence the final list of words will be like 6 million or so.

The process of creating the list is therefor VERY slow. I was thinking if perhaps multithreading could solve my problem. My theory is that I could have say 4 threads (I am new to threads, and do not know if that is possible). In thread 1 I use the first fourth of the dictionary, in thread 2 the second fourth, and so on. Every thread loops through the words in it's part of the dictionary, and add the different combinations. When each thread is finished it would write the result to thread specific text files. When all threads have done their work, I wou开发者_JAVA技巧ld concatenate the different files, into one big text file (with all the 6 million words in it). Thereby cutting processing time in 4. At least that is what I am hoping for. :=)

I am working in C#. Is this possible? So in short: Is it possible to loop through different sections of a text file using different threads in C#? Is there anything in particular I should think about?

I will try to experiment with it, but any advice you might have is highly appreciated.


It's quite possible, assuming that you have a fast means of dividing the list (I assume breaking mid-word would be bad).

But, keep in mind, threads do nothing unless you have spare processing power to put to use. If you are on a single-CPU/single-core PC, you're going as fast as you can. But, if you have multiple CPUs (or at least multiple cores), then this has a chance.

Implementing is fairly simple. If you are currently doing this:

ProcessText(fullTextBlock);

then it's just:

ThreadPool.QueueUserWorkItem(ProcessText, textBlock1);
ThreadPool.QueueUserWorkItem(ProcessText, textBlock2);
ThreadPool.QueueUserWorkItem(ProcessText, textBlock3);
ThreadPool.QueueUserWorkItem(ProcessText, textBlock4);


Since this was my first attempt at using threads, I thought I might share the solution I implemented. If anyone has any suggestions on how to improve this for another time it would be great. My understanding of threads is that it can be a big problem using threads when they are interacting with same methods or variables. But I think that I did separate the threads completely. They are using different instances of a class I created. So if anyone has some good advice on this way of using threads, I am happy. :=) Here is the code I used to render the threads:

 for (int i = 0; i < threads; i++)
            {
                string errorFileName = "errorFile" + (i + 1) + ".dic";
                string saveFileName = "english" + (i + 1) + ".dic";
                string logfileName = "error.log";
                string[] currentContent;
                if (i != threads - 1)
                {
                    currentContent = contentArrayOriginal.Skip(skipStrings).Take(takeStrings).ToArray();
                }
                else
                {
                    int skip = skipStrings;
                    int take = numberOfWords - skip;
                    currentContent = contentArrayOriginal.Skip(skip).Take(take).ToArray();
                }
                PasswordRendering passRender = new PasswordRendering(rootFilePath, errorFilePath, dictionariesFilePath, currentContent, versionsMain, errorFileName, saveFileName, logfileName, (i + 1));
                Thread thread = new Thread(new ThreadStart(passRender.SetPasswords));
                thread.Start();
                skipStrings += takeStrings;
            }

And here is code that saves the rendered passwords (In PasswordRendering class):

File.WriteAllText(dictionariesFilePath + saveFileName, newContent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜