Thread safe moving value between two objects
I have problem. Consider following class:
what i want to do is to thread safe move amount from one "Foo" object, to another that it won't overflow above "MaxAmount".
A bif more context: It is for game for items stacking purpose, it is used server side so two clients at once may attempt to move items.
Please focus on thread safety aspect
11 Replies
... locks?
It's unclear what your problem is. Your post doesn't actually contain a question.
I know that i can use locks, but how? 😂 do you know nice pattern?
Right now i endup with lock spaghetti, but i know someone know nice solution
Had idea to do inventory level lock for items changing purpose
But, items can be stacked across inventories so i endup with same issue just level highier
I think you've over-simplified your question? Your actual problem isn't really related to your post
Either manual locks or the use of Interlocked class
Had idea to introduce some helper method like
public IDisposable EnterWrite()
that i can lock object from outside
What do you think about pattern like this ^Prone to deadlocks, if someone tries to acquire those same locks in the opposite order
Why can't you have one lock for everything? You're never going to be holding it for more than a couple of instructions, so it'll probably never be contended
another pattern could be a queue of actions of players (so that they are serialized)
but it depends on what game is this (like 3d fps, 2d action, textual...)
Maybe 🤔
Something like that seems like it should only ever really be done on a single or "safe" thread, not just any random thread
So if you can, you could invoke your game thread's dispatcher or scheduler (or whatever the equivalent is for what you're using) and use it to invoke the modification on that thread
But if you're saying a client wants to modify that value, then i'd probably just implement a data packet system where client sends something like AddFooPacket
And process those packets on the main/server/game thread
If it's a 3d game for like an inventory/chest management system then it might not be that straight forward, it could be something like PlayerClickGuiPacket, then maybe you invoke an interface method e.g.
IContainer.OnPlayerClick(double x, double y, Foo itemInHand)
or something along those lines (and you'd need some way of serialising/deserialising Foo ofc)
I dunno what that concept is called but the idea is the client and server sync their data via packets (by updating server or being notified of server changes). Not sure if this is what you're going for