Mark Miles
Mark Miles

Reputation: 706

c++ std::future running each function on a separate core

It's a realtime audio application running on a Quad Core Cortex A9 ARMv7 (Linux and gcc 4.8.2), and this is the part where the actual audio is being processed (by calling other functions) and is being sent to the audio driver:

struct STEREOOUT
{
    std::vector<float> L;
    std::vector<float> R;
};

STEREOOUT ProcessEngine(int e, unsigned int sf)
{
    STEREOOUT out;
    for (unsigned int s=0; s<sf; ++s)
    {
        float l, r; 
        engine[e]->Process(l, r);
        out.L.push_back(l);
        out.R.push_back(r);
    }
    return out;
}

// This is the main audio loop callback!
int AudioProcess(void *outputBuffer, unsigned int sampleFrames)
{
    float *buffer = (float*)outputBuffer;
    unsigned int s(0);

    STEREOOUT e0 = async(launch::async, &ProcessEngine, 0, sampleFrames).get();
    STEREOOUT e1 = async(launch::async, &ProcessEngine, 1, sampleFrames).get();

    for (; s<sampleFrames; ++s)
    {
        *buffer++ = e0.L[s] + e1.L[s];
        *buffer++ = e0.R[s] + e1.R[s];
    }

    return 0;
}

How can I make sure that e1 and e2 are running with different affinities from the main() program and yet different between them? For example:

main() -> CPU0
e1 -> CPU1
e2 -> CPU2

My code, when compiled as you see it here, still appears to run on a single core. I need to two audio processing occupy two different cores in order to take advantage of the full power of a quad core processor. Am I just dreaming?

P.S.: I know, I already posted similar questions in the past, but this time I can be more precise because I have a finished and working program now that I can use as an actual example.

Upvotes: 2

Views: 947

Answers (1)

You're calling get immediately after calling std::async, which will cause the program to wait for the result to be available. You need start both async tasks before you wait for them:

auto future1 = async(launch::async, &ProcessEngine, 0, sampleFrames);
auto future2 = async(launch::async, &ProcessEngine, 1, sampleFrames);

STEREOOUT e0 = future1.get();
STEREOOUT e1 = future2.get();

Upvotes: 11

Related Questions