You can download a more complete job processor along with a simple demonstration here.
This example code illustrates most of whats been covered and will provide you with a working example to experiment with. The sample is very simple... the jobs job is to simply wait for a period of time.
As always, you can use this code in your own projects so long as I get a little credit. Its functional and appeared to work well, but it was written specifically for this article so it hasn't been battle tested.
Conclusion
Hopefully, with this first installment, the world of multithreaded programming is now a bit clearer. When I first encountered it I actually couldn't believe how easy it was... and also how easy it was to completely mess up through a lack of understaning with regards to resource protection etc.
My first attempt was a business critical service for one of my employers... it worked great for the first few days... then it crashed. Another few days... *CRASH* Needless to say I didn't score any brownie points for that effort but since then, I've moved on a little. The browser based game I run has its processing service (the ticker). It uses multiple threads to get the best out of the dual CPU machine it runs on. Written as a Windows service, it has the service thread (equivalent to the main VCL thread) which spawns a management thread when it needs to get to work. This in turn spawns 4 different types of thread... one which prepares the database for the tick (one instance), then when that completes, the main workhorse thread (two instances) and finally a cleanup thread (one instance) and the score calculator (one instance). Why am I telling you this?
Well for two reasons...
Firstly, for a bit of moral support if you are struggling with a multithreaded application. My first attempt was a complete failure, but with a bit of perseverance, I've managed to improve my understanding of the subject quite substantially. So much so that now the only time my multithreaded server apps stop working is when the server is shutdown or rebooted. So, if you're having problems stick with it or ask for a bit of help.
And secondly, to try and illustrate whats possible with multithreading. Once you get your head around the potential pitfalls, you can do great things with it. As an example, if our ticker starts to struggle with its workload (if we get lots of players for example), the main workhorse thread has been designed such that it could be farmed out to other servers. The tick preparation thread includes a balancing phase that spreads the workload evenly between a specified number of threads. So... add another dual core server, tell the balancer to spread across 4 threads and then add some software synchronization to manage the two threads on the extra server and I have myself a processing farm.
Anyhow, this has turned out to be a much longer article than I planned so its time for me to go. I hope its made things clearer if you are new to multithreading, but just in case... if you have any questions or you spot an error drop me a mail on athena at outer hyphen reaches dot com and I'll do my best to help you out.
The next installment on multi-threading will cover debugging, throttling and some suggestions regarding tuning for single/multi core machines.
Thanks for reading.... until next time, take care and happy coding

vBulletin Message