C# Class KiloWatt.Runtime.Support.ThreadPoolComponent

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.
Inheritance: Microsoft.Xna.Framework.GameComponent
Show file Open project: sq/Fracture Class Usage Examples

Public Methods

Method Description
AddTask ( TaskFunction function, TaskComplete completion, TaskContext ctx ) : System.Threading.Task

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).

DeliverComplete ( ) : void

Deliver all complete tasks. This is usually called for you, but can be called by you if you know that some tasks have completed.

NewTaskContext ( ) : TaskContext
ThreadPoolComponent ( Microsoft.Xna.Framework.Game game ) : System

Create the ThreadPoolComponent in your application constructor, and add it to your Components collection. The ThreadPool will deliver any completed tasks first in the 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.

Update ( GameTime gameTime ) : void

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Disposing the ParallelThreadPool component will immediately deliver all work items with an object disposed exception.

Private Methods

Method Description
NewWorker ( TaskFunction tf, TaskComplete tc ) : Worker
Reclaim ( TaskContext ctx ) : void
ThreadFunc ( ) : void
WorkOne ( ) : void

Method Details

AddTask() public method

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).
public AddTask ( TaskFunction function, TaskComplete completion, TaskContext ctx ) : System.Threading.Task
function TaskFunction The function to call within the thread.
completion TaskComplete The callback to report results to, or null. If /// you care about which particular task has completed, use a different instance /// for this delegate per task (typically, a delegate on the task itself).
ctx TaskContext A previously allocated TaskContext, to allow for waiting /// on the task, or null. It cannot have been already used.
return System.Threading.Task

DeliverComplete() public method

Deliver all complete tasks. This is usually called for you, but can be called by you if you know that some tasks have completed.
public DeliverComplete ( ) : void
return void

Dispose() protected method

Disposing the ParallelThreadPool component will immediately deliver all work items with an object disposed exception.
protected Dispose ( bool disposing ) : void
disposing bool
return void

NewTaskContext() public method

public NewTaskContext ( ) : TaskContext
return TaskContext

ThreadPoolComponent() public method

Create the ThreadPoolComponent in your application constructor, and add it to your Components collection. The ThreadPool will deliver any completed tasks first in the 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.
public ThreadPoolComponent ( Microsoft.Xna.Framework.Game game ) : System
game Microsoft.Xna.Framework.Game Your game instance.
return System

Update() public method

public Update ( GameTime gameTime ) : void
gameTime Microsoft.Xna.Framework.GameTime
return void