我正在模拟太空中的行星,问题是,我无法模拟超过100颗行星,因为模拟速度呈指数级下降。为了解决这个问题,我认为使用线程可以解决我的问题,因为我可能没有经历过使用图形卡处理器来计算。在行星模拟器中使用boost :: thread
我在我的程序中有2个函数,用来计算行星之间的重力和另一个检查碰撞的重力。我以某种方式实现了线程,因此我计算了一个线程中的重力和另一个线程中的碰撞。
问题是,如果没有使用线程,模拟运行速度不会更快。也许我正在实施他们错了?
int main()
{
int numOfPlanets;
cout << "Enter the maximum number of planets to generate: ";
cin >> numOfPlanets;
App.Create(sf::VideoMode(1366, 740), "SFML Galaxy Simulator");
App.Clear(sf::Color(20,20,20));
generateRandomPlanets(500, 500, numOfPlanets);
//createPlanet(planets, sf::Vector2f(500,500), sf::Vector2f(0,0), 5, 500);
thread thread_1;
thread thread_2;
while(App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
App.Clear(sf::Color(20,20,20));
thread_1 = thread(checkCollision);
thread_2 = thread(calculateForce);
thread_1.join();
thread_2.join();
updatePlanets();
App.Display();
}
thread_2.join();
thread_1.join();
return 0;
}
不要认为*只是带线程*可以提高性能...在数据结构和算法方面考虑。您可能需要进行结构更改才能准备并行执行的应用程序。 – 2013-03-06 20:34:22