문제

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?

도움이 되었습니까?

해결책

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]}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top