Monday, August 15, 2016

PriceList, EnterTownRequest, EconomicSim

For the prototype, I need the server to return a PriceList of buy/sell data when reaching a town. The prices of goods are governed by the EconomicSim (a class that doesn't actually exist yet)--itself a part of WorldState. The WorldState simulation is intended to run on its own thread and expose two interfaces: a read-interface that is thread-safe, and a write-interface that is basically just a ConcurrentQueue for receiving update requests. 

I've started thinking about how to do the thread-safe read interface. When I first started thinking about this problem, I was imagining some kind of double-buffering solution: two copies of WorldState, one used by the readers, and one by the WorldState simulation: after every WorldState useTime, it would swap the two with an interlocked operation. 

That's not quite adequate, though. If the WorldState usetime runs again while receiver threads are still using WorldState from the last tick, you could get an unsynchronized conflict (the read-only version has now become the writable version, and is being scribbled to). I'd need to some explicit locking to prevent this, which defeats the purpose. Maybe I should just use a ReaderWriterLock and design WorldState's update pattern in such a way that the updates are very brief in duration (e.g., doing all the complicated reasoning outside the lock--knowing that our thread is the only thing that can modify the state anyway). 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.