Вопрос

First of all, I'm an Objective-C novice. Most of my background is in Java.. Also since most Objective-C questions revolve around Cocoa, I should point out that this is on GNUStep.

For a school project, I'm creating a simple agent-based-modeling framework. These frameworks are typically used to model complex systems (like the spreading of diseases). My framework features two main objects: a world and a bug. The world consists of "layers", each of which is associated with a toroidal grid. The world can be populated by bugs, and each bug has an x and a y coordinate, and a layer that it belongs to.

My general idea is to populate the world with bugs, and then fire of threads for each of the bugs and let them do what they want. You can create any kind of bug by subclassing the main Bug class and by implementing an act method defined in a protocol. This way you can have various types of custom bugs and custom behavior. Bugs should be able to interact with the world and each other (removing bugs from the world, adding bugs to the world, moving itself around). As you can see, this is quickly headed to multi-threading hell.

Currently I have a bunch of @synchronized blocks and I'm having a hard time ensuring that the world always remains in a consistent state. This is made especially difficult since the bug needs to communicate with and act on the world and vice-versa. I am trying to implement a simple bug called a RandomBug that randomly moves around the world. Even this is proving to be difficult because I'm seeing potential problems where state can become corrupted or invalid.

I started taking a look at NSOperation and NSOperationQueue because it appears that this might make things easier. I have two questions pertaining to this:

  • Is there an easy way to perform NSOperations repeatedly (i.e., at specific intervals).
  • If I set the number of maximum concurrent operations on the thread to 1, do I still need @synchronized blocks? Wouldn't only one thread be interacting with the world at a given time?
  • Is there a better way to tackle this sort of problem (multiple threads interacting with one shared resources in a repeated manner)?
  • Should I forgo threading altogether and simply iterate through bugs on the world and activate them in a random manner?
Это было полезно?

Решение

Sounds like you might want something ala a game/simulation loop... So you have an "update world" phase of your run loop for each time step of your simulation (triggered by an NSTimer), where each bug gets a chance to interact with the world; repeat. Unless your bugs are CPU intensive, this might be the way to go....

As for using NSOperation--sure, this will potentially let you use all your CPU cores, however if there's lots of contention for accessing the world state, this may not be a win after all. In that case, you might try making each tile of your world a separate object you can @synchronized against, reducing contention and allowing better use of your CPU(s).

Using a single NSOperationQueue and setting maxConcurrentOperations = 1 is the same as implementing a game loop, basically. If you use an NSOperationQueue, but don't set maxConcurrentOperations, I'd expect NSOperationQueue to run as many operations simultaneously as you have CPU cores.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top