So I had a strange thought last night for a side project, which I promptly started working on. How many of your are familiar with the concept of thread pools? A thread pool is a pool of threads which are managed by the system and used for doing background work. The basic thought behind them is that you queue up a job to be done on a thread. The caller really doesn't care too much about the state of things since the jobs are meant to be relatively unattended things.
For instance, let's say you were writing a shell for your operating system. When a user drags a folder from one location to another to do a copy, the OS would queue up a work item into the thread pool to do the move. So the user is free to go do other move operations while the first one is operating. It just grabs another thread from the pool.
It's a handy little concept that exists in multiple frameworks, and was relatively easy to create in REALbasic. It's a pretty small amount of source code, but I don't think it'd make for a very good blog entry. Slightly too much code for a blog entry, too little explaining needed to make an article out of it, etc. So one of these days, I may write up a small blurb about it and post it as a freebie somewhere (perhaps on RBLibrary or perhaps on my personal site, or perhaps both. :-P) But I'm curious to know if other people find this sort of thing interesting? Basically, would you use it?
The general use-case for this sort of thing is rather easy. You have a WorkItem (which is an interface, so anything can implement it), and you queue it up in a manager. The manager is a singleton (since the system only needs one manager running) thread, which checks to see if there are any work items waiting to happen. If there are, it grabs a free thread and tells the thread to run the work item. If there are no free threads, the manager just waits until one becomes free.
So it's an entirely automated threading concept. You Queue up a WorkItem and it'll do its work. Simple as that.
Neat? Lame? Thoughts? Suggestions?
You could use WordPress's "pages" to create a static page about it.
Oh definitely. We wrote a whole slew of Queue types which automatically process themselves, but we never got the thread pool version to work properly. Of course, there were a lot of strange threading issues between 5.5.0 and 5.5.5, which is roughly when we gave up on it.
As an aside, we were going to incorporate your Future example into our code but found it didn't work quite the way we had hoped. But we really love seeing your cool code snippets!
Neat in the extreme. Don't worry about the blurb, just put the code somewhere.
You can always write a "how I did it" doc afterwards. That could be followed by an "abuses which caused crashes" doc. ;)
@Will -- eh, I try to keep static content off my blog. I figure if it's static, it belongs on my "main page."
@Daniel -- what sort of issues did you run into with Futures? What way did you hope it to work?
@Steve -- lol, good idea
I would be interested to see how your manager and pool works.
I wrote a HTTPTransaction library which is used in my Net Stakeout and StockLib software. It sounds to be a lot like you are describing, but I used a module as the manager and my "threads" were HTTPSockets.
Definitely would be interested in seeing this.
Also, do you have any more RB patterns articles coming up? The ones thus far have been great!
John
Aaron wrote: "So I had a strange thought last night..."
Dude, that is such a wrong way to start a blog entry! :-)
Russ
I'm only interested if you're able to make those threads in the pools use ALL available CPUs/Copes :^D
Come on, you can do it!
Sure it would be interesting to learn this.
I too would be interested in this... One of my planned projects is a download manager with a transfer queue (user sets how many threads per file, how many files at a time can be downloaded). This would really help with that.
I'm hoping that I got the right idea... What would be nice even before you decide if you are going to post/sell the code and document it, is to maybe give some real life examples of where someone would use this.
@Thomas -- You're correct, I think it is possible (at least on Windows; not certain about other platforms). However, since I don't have a multi-CPU machine, I can't test my thoughts out.
@Scott -- I thought I did. ;-) "For instance, let’s say you were writing a shell for your operating system. When a user drags a folder from one location to another to do a copy, the OS would queue up a work item into the thread pool to do the move. So the user is free to go do other move operations while the first one is operating. It just grabs another thread from the pool."
[...] You should all head over to RBLibrary.com and “purchase” my latest article. Remember how I asked everyone for their thoughts on thread pools? Well, that little side project turned into a full-fledged article. To make things even better — it’s totally free. 100% no-cost-to-you-so-what-are-you-waiting-for? So if you haven’t already grabbed a copy of it yet, I suggest you get over there and snag it; I think it’s a very interesting project and a good read. [...]