2

Consider the following test code:

#include <vector>
#include <array>
#include <future>
#include <iostream>

using std::vector;
using std::array;
using std::cout;
using std::endl;

typedef unsigned int uint;


double TestFutures()
{
    return 1;
}

void DoWork()
{
    const uint nPoints=50;
    const uint nThreads=100;
    vector<double> results(nThreads,0);

    // For each data point...
    for (uint k=0; k<nPoints; ++k) {
        // ... launch the threads
        vector<std::future<double> > values;
        for (uint w=0; w<nThreads; ++w) {
            values.push_back(std::async(TestFutures));
        }
        // ... and now read the results
        for (uint w=0; w<nThreads; ++w) {
            results[w]+= values[w].get();
        }
    }
}

int main()
{
    const uint nTimes=50;

    for (uint k=0; k<nTimes; ++k) {
        cout << "Cycle: " << k << endl;
        DoWork();
    }
}

I compile it in Qt 5.0.1, MinGW 4.7.2, Qt Creator 2.6.2. Then I run it, either in the debugger, or stand alone, release or debug version. If I monitor the memory usage, it increases monotonically during the execution of the loops. By increasing the variable nTimes in the main function, I can make the program allocate an arbitrary amount of memory (I have tried up to 1Gb). I monitor the memory usage with task manager or procexp.

If I then take the same code and compile it and run it under MS Visual Studio Express 2012, the memory usage remains stable during the loop. This, I think, is what you would expect, as the vector of futures should be destroyed after each k-iteration.

I am not sure, but I believe the problem then lies on Qt or MinGW. Actually, I suspect it might be a problem with MinGW. But before I report it to the developers, I would like to ask other kind users to verify this issue. (I'd love to be wrong!)

Just to let you know, I still have the same issue if I substitute the vector of futures with an array, i.e. this DoWork() routine:

void DoWork()
{
    const uint nPoints=50;
    const uint nThreads=100;
    vector<double> results(nThreads,0);

    // For each data point...
    for (uint k=0; k<nPoints; ++k) {
        // ... launch the threads
        array<std::future<double>,nThreads> values;
        for (uint w=0; w<nThreads; ++w) {
            values[w]=std::async(TestFutures);
        }
        // ... and now read the results
        for (uint w=0; w<nThreads; ++w) {
            results[w]+= values[w].get();
        }
    }
}
4

0 回答 0