Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

A spinning loop thread, attach and detach

This topic is 2630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are there any good references/books/sites that discuss how to create a looping thread so
that any objects can attach to it while not affecting the rest of the program?
Thank you very much
Jack

Share this post


Link to post
Share on other sites
Advertisement
I'd want to run independent threads for pathfinding. So that I can keep my program responsive. otherwise, the app will freeze when performing tons of pathfinding in the foreground. Thanks a lot :) Jack

Share this post


Link to post
Share on other sites

Are there any good references/books/sites that discuss how to create a looping thread so
that any objects can attach to it while not affecting the rest of the program?
Thank you very much
Jack


Perhaps the reason you're not getting more traction on this question is we don't really know enough. What language are you using? What operating system?

For examples, if it's C++ and Windows - and since you've asked for pointers for further research - I might suggest you go and look up, as a simple starting point, Critical Sections on MSDN, followed by the function InitializeCriticalSection (and the other functions mentioned on that page). There are 1,001 ways to skin a cat, and that's just one - but it'll do what you want...

...which is to have either thread only touch the memory they share in a mutually exclusive fashion. Consider the following pseudocode...
[source lang="cpp"]
//ui loop
while(true) {
lock(thread_critical_section);
ui_localdata = shared_data;
unlock(thread_critical_section);
render(ui_localdata);
}

//thread loop
while(true) {
thread_localdata = calculate(pathinfo);
lock(thread_critical_section);
shared_data = thread_localdata;
unlock(thread_critical_section);
}
[/source]

Share this post


Link to post
Share on other sites
What you're looking for is called a "job queue" See for example here or google for that term.

Basically it's an implementation of the proactor pattern with "events" being the tasks (pathfinding in your case) to perform. If you're programming under Windows, this can be done very nicely and efficiently using a completion port and a handful of worker threads.

Share this post


Link to post
Share on other sites
If you use locking, you could end up with the threads interfering with the main program. If you do it locklessly, and you don't have any "spare" cores, the pathfinding computations might impact on your game too, if you are running close to your CPU-per-frame budget. Threading is a complex beast, I would avoid it until I had exhausted other options.

One alternative is to write your path-finding algorithm so it can run over many frames.

Another alternative to locking, if the pathfinding can handle stale information, is to make a deep copy of all the data needed, and pass that copy to the thread.

As for your question, it sounds like you want some kind of thread pool, where objects can enqueue pathfinding tasks into, and later poll or be notified when the task completes.

Share this post


Link to post
Share on other sites

I'd want to run independent threads for pathfinding. So that I can keep my program responsive. otherwise, the app will freeze when performing tons of pathfinding in the foreground. Thanks a lot :) Jack


A pathfinding algorithm requires three pieces of information: start point, end point, and map data. It would then perform the calculation and spit out a new Path object. The calculations can be performed independent of the rest of your objects. You don't need to do any copying, acquiring locks for all objects, synchronizing states, and so on.

If you have a lot of objects moving around such as an RTS game, and each object can obscure pathing of other objects, then the only thing you need to be careful of is synchronizing your map data. Your pathfinding thread should acquire a readonly lock to use map data, and you don't have to do this in every frame whenever a unit moves. Only do this if there's a unit unable to complete its original path and requires a new pathing because other units are in the way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!