0

I wonder if I got it all wrong when I have tried to used concurrent_queue. I am trying to process files using threads. The Threads pickup file names from a concurrent_queue and proceed ahead. My problem is that each thread seems to process the same file 4 times as I have 4 threads.

My main goal was to pick 4 different files from the queue and process them independently till the queue gets exhausted.

#include <string>
#include <strstream>
#include <ppl.h>
#include <concurrent_queue.h>
#include <thread>
using namespace std;
using namespace concurrency;

void ProcessQ(concurrent_queue<string> &fileQ,string folder)
{
    TxtFileReader reader;
    while (!fileQ.empty())
    {
        string fileName;
        if (fileQ.try_pop(fileName))
        {
            vector<float> fpTemplate(0);
            int n = reader.ReadTxtFile(folder+fileName, fpTemplate);
            if (n > 0)
            {
                cout << "Processed file:" << fileName<<endl;
            }
            else
                cout << "Skipping file:" << fileName<<endl;
        }

    }
}
int main()
{

    stringstream fileNameStream;
    concurrent_queue<string> fileQ;
    for (int first = 1; last<= 100; first++)
    {
            fileNameStream << first << ".txt";      
            fileQ.push(fileNameStream.str());                       
            fileNameStream.str(string());
    }

    string folder = string("E:\\Tests\\Outputs\\txts\\");

    // Create threads and exectue
    const short nThreads = 4;
    thread fileProcessorThreads[nThreads];

    for (short i = 0; i<nThreads; i++) 
    {
        fileProcessorThreads[i] = thread(ProcessQ,fileQ,folder);
    }
    for (auto& th : fileProcessorThreads) {
        th.join();
    }


    return 0;
}

}

The output on console is

Processed 1.txt Processed 1.txt Processed 1.txt Processed 1.txt Processed 2.txt Processed 2.txt Processed 2.txt Processed 2.txt

What am I doing wrong?

Wajih
  • 793
  • 3
  • 14
  • 31

1 Answers1

1

Got it, I have to use std::ref() in order to make a shared reference to the queue.

fileProcessorThreads[i] = thread(ProcessQ,std::ref(fileQ),folder);

The original code was probably sending a copy of the queue to the thread functions.

Wajih
  • 793
  • 3
  • 14
  • 31