volatile directories and threading

05/25/2013 18:41 go for it#1
is it possible to run hunting loop at separate thread using volatile directories updating by main thread ?
forgive my ignorance i've never done this , is it the best way to get it done ? any information would be highly appreciated , have a great day
05/25/2013 20:13 pro4never#2
I might be completely misunderstanding what your question is but what exactly are you trying to accomplish.

I see no reason to have multiple threads each handling one small portion of a hunting loop (targeting loop, attacking loop, looting loop etc should generally not be on their own threads but w/e)


Concurrent collections are incredibly useful for synchronizing data between multiple threads (EG: collection of monsters on screen which might be edited by a spawn packet while your bot code is iterating it for hunting)

Volatile objects as I understand them (haven't had a reason to use them much myself) force the compiler to get the most recently updated value every time which would make it very useful for variables accessed from multiple threads.


I think the real question is what are you doing for thread safety to start with? These are all very powerful tools but using them as an excuse to organize your threading poorly is just a crutch in my eyes.


PS: What is a 'volatile directory'? Volatile in C# is a keyword and does not relate to a directory in any way that I'm aware of.
05/25/2013 20:16 Spirited#3
What you might want to consider doing is locking parts that aren't thread safe (such as methods that rely on data structures or classes from .NET that aren't thread safe). Be very careful with locking though. That was a major problem in TrinityCO (and still is with the public version) - deadlocks (two or more actions are each waiting for the other to be executed). Use minimal locks (in a safe manor that won't cause deadlocks) and try relying on thread-safe data structures such as a Concurrent Dictionary from .NET's framework.
05/25/2013 20:49 go for it#4
what i want to do is having a thread hunting while the main thread is updating my collections (monster coords , etc)
ill look up the Concurrent collections thanks pro4never
and thanks fang for the lock hint ill look it up too

edit : btw pro4never you was invoking delegates to update the gui packet textbox from the handlers class at proxyparadise , do you think this is the best way to do this or should i pass the gui instance throw ctor/custom method to the handlers class to update the gui on receiving packets ?
05/25/2013 22:04 pro4never#5
I'm always very bad with how I handle cross thread calls but I seem to remember that the 'correct' way to do it is delegates. Basically they let you pass a command to be executed on the target thread (in this case the GUI thread)


In your description you would have your packets being processed asynchronously which would update a concurrent collection (likely dictionary of uid/entity). You would then have a logic thread running through that collection to perform actions. Concurrent collections are perfect for this and nothing further is required from your side.



The alternative would be asynchronous packet receiving which places each packet into a ConcurrentQueue (incoming/outgoing queue of packets). You then have a single thread which manages everything related to that client and each loop it processes packets, updates objects and then performs bot actions. In this example there is no need for thread safe collections for the client's surroundings as ALL client logic is on one single thread. You still have to handle the original packet processing in a thread safe way but I think it might work best from a bot standpoint because you can easily control things like keeping the bot from flooding the server/client with packets.