I try to understand tasks in intel tbb. I try to create a parallel algorithm to solve the langfords paring for two "blocks" L(2,n)(<https://en.wikipedia.org/wiki/Langford_pairing>)

My algorithm works when I call sequential, but I want to translate it in tasks. This is what my algorithm should do:

* make a vector in size 2\*n, init with "0"
* for 0 untill (1 + loopcounter + distance-of-blocks) < size of the vector do:
* duplicate the given vector
* add the current block on loopcounter
* add the current block on 1 + loopcounter + distance-of-blocks
* if this is NOT the last block:
* do the same in a task with block-difference -1 and the duplicated already filled vector
* else, check if there are "0" left
* if not, this is a valid solution

I currently ignore symmetry

This is the current code

```
int langford_task ( int step, vector<int> v)
{
task_group g;
int counter = 0;

cout << "current step: "<< step << endl;
cout << "current vector in task: ";

printVector(v);

//'1+var+step' == 1 + our loopcounter + the distance of two 'blocks'
for ( unsigned int var = 0; 1+var+step < v.size(); ++var )
{

    if ( v[ var ] == 0 && v[ 1+var+step ] == 0 )
    {
        vector<int> recV = v;
        recV[var] = step;

        recV[1+var+step] = step;

        cout << "recV = ";
        printVector(recV);
        if ( step - 1 != 0 )
        {
            //make a task with step -1 and the new filled vector
            g.run( [&]{ counter += langford_task( step-1, recV); } ); //spawn a task
        }
        else
        {
            // if there is no "0" in our vector, we found a valid solution and return 1
            if( !( std::find( recV.begin(), recV.end(), 0) != recV.end() ) )
                return 1;
        }
    }

}
g.wait(); //wait for tasks

return counter;
}

```

In theory, the task\_group shall wait in the end of the foor-loop, so all the child-tasks can finish first.

I print the vector, so I can see what is inside, and thats kinda odd:

```
current step: 3
current vector in task: [0, 0, 0, 0, 0, 0, ]
recV = [3, 0, 0, 0, 3, 0, ]
recV = [0, 3, 0, 0, 0, 3, ]

```

everything is normal, until the task comes

```
current step: 2
current vector in task: [28848976, 0, 0, 0, 0, 3, ]
recV = [28848976, 2, 0, 0, 2, 3, ]
current step: 1

```

this is absolutely odd. I have to mention that "28848976" seems to be a random number. It is always different, most of the time its "0"

I expected for "current vector in task:" in the "current step: 2"-section

```
[3, 0, 0, 0, 3, 0, ]

```

because this is just the parameter i have given to this function.

It "works" when I add the 
 g.wait(); //wait for tasks

directly under the 

```
g.run(...)

```

but this consumes even more execution-time then working without tasks at all and is probably not parallel anymore.

```
current step: 3
current vector in task: [0, 0, 0, 0, 0, 0, ]
recV = [3, 0, 0, 0, 3, 0, ]
current step: 2
current vector in task: [3, 0, 0, 0, 3, 0, ]
recV = [3, 0, 2, 0, 3, 2, ]
current step: 1
current vector in task: [3, 0, 2, 0, 3, 2, ]
recV = [3, 1, 2, 1, 3, 2, ]

```

Why is the task behaving so strangely? What can I do to get it running?

Just for completion, the rest of the code:

```
void printVector( vector<int> v )
{
    cout << "[";
    for ( unsigned int var = 0; var < v.size(); ++var ) {
        cout << v[var] << ", ";
    }
    cout << "]" << endl;
}

void langford_parallel( int s, int n )
{
    cout << "berechne Langenford-Sequenz für S = " << s << " und N = " << n << endl;

//  concurrent_vector<int> v( ( s*n ), 0);
    vector<int> v( ( s*n ), 0);

    int solutions = 0;
    solutions = langford_task(n, v);

    cout << "found solutions: " << solutions << endl;
}

int main()
{
    tick_count t0 = tick_count::now();
//  langford_sequentiell( 2, 12 );
    langford_parallel( 2, 3 );
    tick_count t1 = tick_count::now();

    cout << "work took " << (t1-t0).seconds() << " seconds." << endl;
    return 0;
}

```