Visual c++ 如何利用PPL实现while循环的并行化

Visual c++ 如何利用PPL实现while循环的并行化,visual-c++,parallel-processing,ppl,Visual C++,Parallel Processing,Ppl,我需要通过PPL并行化while循环。在Visual C++中,我在MS VS 2013中有以下代码。 int WordCount::CountWordsInTextFiles(basic_string<char> p_FolderPath, vector<basic_string<char>>& p_TextFilesNames) { // Word counter in all files. atomic<unsigned&g

我需要通过PPL并行化while循环。在Visual C++中,我在MS VS 2013中有以下代码。
int WordCount::CountWordsInTextFiles(basic_string<char> p_FolderPath, vector<basic_string<char>>& p_TextFilesNames)
{
    // Word counter in all files.
    atomic<unsigned> wordsInFilesTotally = 0;
    // Critical section.
    critical_section cs;

    // Set specified folder as current folder.
    ::SetCurrentDirectory(p_FolderPath.c_str());

    // Concurrent iteration through p_TextFilesNames vector.
    parallel_for(size_t(0), p_TextFilesNames.size(), [&](size_t i)
    {
        // Create a stream to read from file.
        ifstream fileStream(p_TextFilesNames[i]);
        // Check if the file is opened
        if (fileStream.is_open())
        {
            // Word counter in a particular file.
            unsigned wordsInFile = 0;

            // Read from file.
            while (fileStream.good())
            {
                string word;
                fileStream >> word;
                // Count total number of words in all files.
                wordsInFilesTotally++;
                // Count total number of words in a particular file.
                wordsInFile++;
            }

            // Verify the values.
            cs.lock();
            cout << endl << "In file " << p_TextFilesNames[i] << " there are " << wordsInFile << " words" << endl;
            cs.unlock();
        }
    });
    // Destroy critical section.
    cs.~critical_section();

    // Return total number of words in all files in the folder.
    return wordsInFilesTotally;
}
这段代码通过外部循环中的std::vector进行并行迭代。并行性由算法的concurrency::parallel_提供。但这段代码也有嵌套的while循环,它执行从文件读取的操作。我需要并行化这个嵌套的while循环。这个嵌套while循环如何通过PPL实现并行化。请提供帮助。

正如用户在其评论中所提示的那样,从同一ifstream实例进行并行读取将导致未定义和不正确的行为。有关更多讨论,请参阅问题。使用这个特定的算法,您基本上处于并行化的极限


作为旁注,即使并行读取多个不同的文件流,如果它们都是从同一物理卷读取的,也不会真正加快速度。磁盘硬件实际上只能支持如此多的并行请求,通常一次不超过一个,在忙时对任何传入的请求进行排队。要了解更多的背景知识,你可能想看看马克·弗里德曼的;性能计数器是特定于Windows的,但大多数信息都是通用的。

在您花费大量时间尝试并行化while循环之前,请问问自己,您的硬件如何支持从单个文件并行读取。我需要并行读取文本文件。帮助