Improved XNA Thread Pool object

jwatte's picture

This post includes a thread pool class for the XNA Framework that works better on the Xbox than the thread pool class in the Compact CLR. The main thread can choose to get notified about task completion with a callback function, or by waiting on a pre-allocated wait handle, or do fire-and-forget on the thread tasks.

You typically only create a single ThreadPoolComponent in your application, and let all your threaded tasks run within this component. This allows for ideal thread balancing. If you have multiple components, they will not know how to share the CPU between them fairly.

Create the ThreadPoolComponent in your application constructor, and add it to your Components collection. The ThreadPool will deliver any completed tasks first in the component update order.

On Xbox, creates 3 threads. On PC, creates one or more threads, depending on the number of CPU cores. Always creates at least one thread. The thread tasks are assumed to be computationally expensive, so more threads than there are CPU cores is not recommended.

Add a task to the thread queue. When a thread is available, it will dequeue this task and run it. Once complete, the task will be marked complete, but your application won't be called back until the next time Update() is called (so that callbacks are from the main thread). You can pass in a previously allocated TaskContext, to allow for waiting on the task, if you want finer grain synchronization than Game.Update() callbacks.

Finally, this thread pool is engineered so that it will not generate any garbage during normal runtime, which is important when running under the Compact CLR used on the Xbox.

Update: Through code inspection, I found a bug in the handling of exceptions thrown during task complete delivery. (this bug has never actually hit me, because I don't throw during completion callbacks) It might lose some notifications in that case. The code in question is:

            completeListEnd_ = w.next_;
            throw new System.ApplicationException("The thread pool user threw an exception on delivery.", x);

It should look something like:

            completeListEnd_ = w.next_;
            while (completeListEnd_.next_ != null)
              completeListEnd_ = completeListEnd_.next_;
            throw new System.ApplicationException("The thread pool user threw an exception on delivery.", x);

Because someone asked, I've made this thread pool available under the MIT open source license. The copyright holder is Jon Watte, and the date is 2008. If MIT license doesn't work for you, please let me know, and we'll work something out.

Copyright © 2008 Jon Watte

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AttachmentSize
ParallelThreadPool.zip3.38 KB