You are here: Magnum » Documentation »

Fibers

Fibers

The Fibers namespace enables the development of asynchronous applications.

Fiber

A Fiber is a building block used to create classes that operate on themselves in a single execution context. By executing all methods on an object in a single execution context, the class does not need to use complex locking mechanisms to prevent threading issues. This is useful when creating a large number of autonomous fine-grained objects that interact via an asynchronous communication medium.

The preferred implementation of Fiber is the ThreadPoolFiber. By using the default .NET ThreadPool, threads are only used when they are needed and available. There is not a single thread dedicated to the queue. This is the most scalable version available since it shares the thread pool across every instance, reducing the memory requirements significantly.

The less preferred but sometimes necessary implementation is the ThreadFiber. This version uses a dedicated thread for each instance. While this is less scalable and uses significantly more system resources per instance, there are certain cases where a dedicated thread is needed to ensure that processing time is allocated for the queue.

The last implementation is the SynchronousFiber, which is purely designed for unit testing so that asynchronous issues do not have to be dealt with in unit tests. In this implementation, queued actions are executed immediately on the calling thread with no asynchronous execution available.

Scheduler

Scheduler provides a mechanism for scheduling actions to be executed at a later time. At the scheduled time, an action is placed onto the Fiber specified when the action is scheduled so that it can be executed within the context of the owner of the Fiber.

The default implementation of the Scheduler is the TimerScheduler. It uses a System.Threading.Timer to callback when the next scheduled action is ready to be executed. The only activity performed on the timer callback thread is enqueuing the scheduled action onto the specified Fiber. Once complete, the timer is rescheduled for the next scheduled action.