I'm having a software project that needs to communicate to different network hosts and react on events like UI events, various network messages from different sockets, timers, ...
My problem is that I cannot find a satisfying solution to ensure thread safety while still offering non-blocking methods. I have classes that kind of all depend from each other and in my imagination I see lots of threads running uncontrolled cross-country through my classes's methods. So I'd have to create locks for just everything.
I think by adding too many locks my methods could almost behave like blocking ones and produce errors like dead locks.
I need to use .NET 3.5, but I'd be curious what the solution to this looks like in any language / framework (maybe using .Net > 3.5 would be the solution?)
At first I tried imagining the flow of my threads that are created by using Begin...() methods. So my first approach was to define a hand full of threads that maintain a certain part of my application (e.g. Network thread, Event thread, ... ). That way I theoretically could reduce the need of locks as resources are bound to their only one thread.
As those threads had to care about different actions, I tried implementing my own event queue (see How to implement a message pump in Non-UI thread in .NET?) but feel like if .Net does not offer at least some sort of pattern for that purpose, there is likely a better solution to those problems.
I keep on reading all the MSDN pages about design patterns etc, which helps, but still I don't see the whole picture.
I think this is a common scenario in .Net or concurrent programming in general. Maybe I'm just not seeing the solution that resides right in front of me. If so I'd be happy if someone could point me into the right direction.
It is a complex topic and hard to explain. Hope I could give sufficient information. If anything is unclear, please ask.
Thank you for your help.
Aucun commentaire:
Enregistrer un commentaire