Click here to Skip to main content
15,879,326 members
Articles / General Programming / Threads

Declarative multithreading

Rate me:
Please Sign up or sign in to vote.
4.94/5 (39 votes)
13 Mar 2012CDDL19 min read 57.4K   864   139  
An introduction and proof of concept code for the idea of declarative multi threading in C#.
using System.Threading;
using System;

namespace ThreadBound
{
    /// <summary>
    /// Synchronization context for the worker thread context. If a class is bound to a thread via the
    /// [ThreadBound]-attribute the class will be executed within its own worker thread. This class
    /// implements the synchronization context for the Worker class. It will be automatically created
    /// if a Worker instance is created.
    /// </summary>
    internal class WorkerSynchronizationContext :
        SynchronizationContext,
        IDisposable
    {
        /// <summary>
        /// Reference to the worker that is used for executing the methods.
        /// </summary>
        private Worker AssignedWorker;

        /// <summary>
        /// C'tor
        /// </summary>
        /// <param name="workerToUse">Reference to the worker that will execute the callbacks passed to this synchronization context.</param>
        public WorkerSynchronizationContext(Worker workerToUse)
        {
            AssignedWorker = workerToUse;
        }

        /// <summary>
        /// Overrides the Post method in SynchronizationContext.
        /// </summary>
        /// <see cref="SynchronizationContext.Post"/>
        public override void Post(SendOrPostCallback d, object state)
        {
            AssignedWorker.EnqueAsync(d, state);
        }

        /// <summary>
        /// Overrides the Send method in SynchronizationContext.
        /// </summary>
        /// <param name="d"></param>
        /// <see cref="SynchronizationContext.Send"/>
        public override void Send(SendOrPostCallback d, object state)
        {
            AssignedWorker.EnqueSync(d, state);
        }

        /// <summary>
        /// Overrides the CreateCopy of the SynchronizationContext.
        /// </summary>
        /// <see cref="SynchronizationContext.SynchronizationContext"/>
        public override SynchronizationContext CreateCopy()
        {
            return new WorkerSynchronizationContext(AssignedWorker);
        }

        /// <summary>
        /// Wird der Synchronisationskontext abgebaut, so wird der zugewiesene Worker angehalten.
        /// </summary>
        public void Dispose()
        {
            AssignedWorker.Stop();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Systems Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions