Question

I have a game server that spawns a thread for each client with forkIO. I want to, for example, share a list of clients and a list of monsters with all of them.

My first idea was to have one TVar container for a data type containing both lists, but say two threads changes a client and monster at the same time, that would rollback one of them, if I understood that part correct. Which would be an unnecessary rollback.

The second idea was to make the lists TVars and then just pass them as arguments, but I'd rather have them in a container to keep the code clean and easily managed if more lists are to be added.

Is there a way to pass a couple of TVars in a container or am I overthinking the first idea?

Was it helpful?

Solution

How about just creating a containing data type that contains 2 TVars? This isn't much different from just passing the TVars as separate arguments, but it keeps them "packaged" together, which you seem to prefer.

data GameState = GameState {clients :: TVar [Client], monsters :: TVar [Monster]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top