Thread: A Challenge in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> Probably because if you have lots of ants, you could end up starting too many threads.
    My computer's running ~1000 threads right now and since I only plan to have 5 - 20 ants, I figured by program's number of threads would be an after thought. I'm not very experienced with high thread counts so I may be wrong. If I do decide to take the simulation farther, I'd see if I could use one of the college's systems with 128 4-core processors.
    Of course, if it's just bad programming practice, let me know and I'll find another way.

    >> A lot of the code is event-based, too, and a manager classes/objects are used frequently to pass events down the chain.
    The manager class will be event based where the ants (movement, interacting with objects) and time invervals (check for starvation, food spawn, etc..) initiate the events.

    >> your governing (or manager) class shouldn't decide when an ant dies
    The governing class is designed to act as nature and make sure that the ants obey its laws. I figured since an ant can't decide to just not die of starvation, it shouldn't be the ants choice.

    As a final note: I may be going overboard on threads. I've never written a program that had a decent reason for multi-threading, so I may be over-eager to use them. Oh, and I have basically no knowledge of proper uses of threads seeing as I learned all I know from about 10 pages of "Tricks of the Game Programming Gurus" by Andre LaMothe.
    Don't quote me on that... ...seriously

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brad0407 View Post
    >> Probably because if you have lots of ants, you could end up starting too many threads.
    My computer's running ~1000 threads right now and since I only plan to have 5 - 20 ants, I figured by program's number of threads would be an after thought. I'm not very experienced with high thread counts so I may be wrong.
    The problem is that the world state has to be properly synchonized with what the ants are doing. There are two basic ways of designing it:

    1. Computation is divided into two serial operations. First, the ants all "think" and decide what their new states will be. Once ALL the ants have done this, then the world state is updated to reflect the changes in some consistent way. This requires very little locking during the "thinking" stage, provided the ants do not interact much with each other while they think. But the disadvantage is that all the ants must wait until the SLOWEST ant finishes thinking before any changes to the world state can be applied.

    2. Computation is an ongoing, concurrent process of ants thinking and updating the world state as they make decisions, each at its own rate. This requires a whole bunch of locking and resource notification to maintain consistent world state between all the updates the threads are doing. This is not only less efficient (than if you didn't need to lock), but much more difficult to code correctly.

    So you basically have to choose which method has advantages which outweigh the disadvantages. You might not be able to discover this a priori so you might as well just pick one approach, try it, and tell us how it goes.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by brewbuck View Post
    The problem is that the world state has to be properly synchonized with what the ants are doing. There are two basic ways of designing it:

    1. Computation is divided into two serial operations. First, the ants all "think" and decide what their new states will be. Once ALL the ants have done this, then the world state is updated to reflect the changes in some consistent way. This requires very little locking during the "thinking" stage, provided the ants do not interact much with each other while they think. But the disadvantage is that all the ants must wait until the SLOWEST ant finishes thinking before any changes to the world state can be applied.

    2. Computation is an ongoing, concurrent process of ants thinking and updating the world state as they make decisions, each at its own rate. This requires a whole bunch of locking and resource notification to maintain consistent world state between all the updates the threads are doing. This is not only less efficient (than if you didn't need to lock), but much more difficult to code correctly.

    So you basically have to choose which method has advantages which outweigh the disadvantages. You might not be able to discover this a priori so you might as well just pick one approach, try it, and tell us how it goes.
    I'm going to try numero 2. I'm going to have the central class govern the laws of nature. I'm also going to use it to keep the internal states of all the ants. All the ants will be able to think whenever they please, but only one will be allowed to change their state at a time because all state changes must be cleared through the central class. Does that sound like a good plan or not?
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AI Challenge
    By unknown_111 in forum General AI Programming
    Replies: 0
    Last Post: 10-02-2007, 12:18 AM
  2. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:56 AM
  3. Calc challenge
    By cerin in forum C++ Programming
    Replies: 5
    Last Post: 02-06-2005, 04:57 PM
  4. Challenge?
    By Mackology101 in forum C Programming
    Replies: 5
    Last Post: 11-23-2004, 02:30 PM
  5. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM