Генерация потоков в цикле for - PullRequest
1 голос
/ 26 апреля 2020

Я пытаюсь разделить обнаружение столкновений на отдельные theads, чтобы повысить эффективность. Код, который у меня сейчас есть:

/*CHECK COLLISION HERE*/

    std::thread threads[6];

    Vector2 birdPointsArr[6];
    Vector2* birdPoints = getBirdPoints(birdPointsArr);
    //check each pipe
    for (int i = 0; i < 6; i++)
    {
        //only check pipes with viable x coords
        if (pipes[i]->getX() < birdPoints[2].x + 20 && pipes[i]->getX() + pipes[i]->getW() > birdPoints[4].x - 20)
        {
            //check both upper and lower pipe   
            for (int j = 0; j < 2; j++)
            {
                Vector2 pipePointsArr[4];
                Vector2* pipePoints = getPipePoints(pipePointsArr, i, j);

                //only check pipes with viable y coords
                if (j == 0 && birdPoints[0].y > pipePoints[2].y + 20)
                {
                    continue;
                }

                if (j == 1 && birdPoints[3].y < pipePoints[0].y - 20)
                {
                    continue;
                }
                threads[i] = std::thread([&]() {
                    //if x and y coords are viable, check collision
                    if (collisionDetection(birdPoints, pipePoints)) {
                        dead = true;
                    }
                });
            }
        }
    }

    for (int i = 0; i < 6; i++)
    {
        if(threads[i].joinable())
            threads[i].join();
    }

Меня беспокоит то, что я создаю и уничтожаю несколько потоков в каждом кадре. Я обеспокоен тем, что все эти создания и разрушения делают мой код медленнее, чем если бы я оставил его синхронным; однако я не знаю, как это измерить. Было бы быстрее, если бы я просто вернул его к серийному виду?

...