开发者

.net 4.0 Tasks: Synchronize on one or more objects

I have read a lot about the new Task functionality in .net 4.0, but I haven't found a solution for the following problem:

I am writing a server application that processes requests from many users and I want to use Tasks to distribute these request on multiple cores. However, these Tasks should be synchronized on objects - for the beginn开发者_JAVA技巧ing, users -, so that just one task is processed for each object at a time. This would be simple to achieve with Task.ContinueWith(), but it should also be possible to synchonize a task on multiple objects (e.g. when a user transfers money to another user, a variable should be decremented at user A and incremented at user B without other tasks interfering).

So, my first attempt is a class that receives delegates, creates tasks and stores them in a dictionary with the objects to sync on as keys. If a new task is scheduled, it can be appended to the last task of the given object with Task.ContinueWith(). If it should be synchronized on multiple objects, the new Task is created using TaskFactory.ContinueWhenAll(). The created task is stored in the dictionary for every object it is synchronized on. Here is my first draft:

 public class ActionScheduler:IActionScheduler
{
    private readonly IDictionary<object, Task> mSchedulingDictionary = new Dictionary<object, Task>();
    private readonly TaskFactory mTaskFactory = new TaskFactory();

    /// <summary>
    /// Schedules actions synchonized on one or more objects. Only one action will be processed for each object at any time.
    /// </summary>
    /// <param name="synchronisationObjects">Array of objects the current action is synchronized on</param>
    /// <param name="action">The action that will be scheduled and processed</param>
    public void ScheduleTask(object[] synchronisationObjects, Action action)
    {            
        // lock the dictionary in case two actions are scheduled on the same object at the same time
        // this is necessary since reading and writing to a dictionary can not be done in an atomic manner
        lock(mSchedulingDictionary)
        {
            // get all current tasks for the given synchronisation objects
            var oldTaskList = new List<Task>();
            foreach (var syncObject in synchronisationObjects)
            {
                Task task;
                mSchedulingDictionary.TryGetValue(syncObject, out task);
                if (task != null)
                    oldTaskList.Add(task);
            }

            // create a new task for the given action
            Task newTask;
            if (oldTaskList.Count > 1)
            {
                // task depends on multiple previous tasks
                newTask = mTaskFactory.ContinueWhenAll(oldTaskList.ToArray(), t => action());
            }
            else
            {
                if (oldTaskList.Count == 1)
                {
                    // task depends on exactly one previous task
                    newTask = oldTaskList[0].ContinueWith(t => action());
                }
                else
                {
                    // task does not depend on any previous task and can be started immediately
                    newTask = new Task(action);
                    newTask.Start();
                }
            }

            // store the task in the dictionary
            foreach (var syncObject in synchronisationObjects)
            {
                mSchedulingDictionary[syncObject] = newTask;
            }
        }
    }
}

This even works if a task "multiSyncTask" was created for multiple objects, and afterwards tasks for each of the objects are scheduled. Since they are all created with multiSyncTask.ContinueWith(), they start synchronously:

static void Main()
    {
        IActionScheduler actionScheduler = new ActionScheduler();

        var syncObj1 = new object();
        var syncObj2 = new object();

        // these two start and complete simultaneously:
        actionScheduler.ScheduleTask(new[] { syncObj1 }, () => PrintTextAfterWait("1"));
        actionScheduler.ScheduleTask(new[] { syncObj2 }, () => PrintTextAfterWait("2"));
        // this task starts after the first two and "locks" both objects:
        actionScheduler.ScheduleTask(new[] { syncObj1, syncObj2 }, () => PrintTextAfterWait("1 and 2"));
        // these two - again - start and complete simultaneously after the task above:
        actionScheduler.ScheduleTask(new[] { syncObj1 }, () => PrintTextAfterWait("1"));
        actionScheduler.ScheduleTask(new[] { syncObj2 }, () => PrintTextAfterWait("2"));
    }

    static void PrintTextAfterWait(string text)
    {
        Thread.Sleep(3000);
        Console.WriteLine(text);
    }

What do you think - is this a good solution for my problem? I am a bit sceptic about the big lock on the dictionary, but it is necessary in case two tasks are scheduled on one object at once to prevent race conditions. Of course, the dictionary is just locked for the time it takes to create a task, not when it is processed.

Also, I would love to know if there are any already existing solutions or coding paradigms out there that solve my problem better using .net 4.0 Tasks that I have failed to track down.

Thank you and with best regards, Johannes


If I got you right.. you would like to have a Task.ContinueWith(task1, task2, lambda)? Something like the Join arbiter in CCR? http://msdn.microsoft.com/en-us/library/bb648749.aspx If so, probably the most elegant option is to use the JoinBlock in TPL dataflow (http://www.microsoft.com/download/en/confirmation.aspx?id=14782). Or, maybe, have you tried to do use Task.WaitAll() as the first instruction of your dependent task?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜