Home > .Net > .Net 4.0 first tasting – Task Parallel Library (TPL)

.Net 4.0 first tasting – Task Parallel Library (TPL)

Finally our project was “upgraded” to VS2010 and .Net 4.0, we was waiting anxiously for that event.
In first opportunity I have tried to use new language/Framework features like Parallel.Foreach and PLINQ.

My task was to aggregate duration time of recorded stuff (it doesn’t really matter what data I needed to sum). In order to retrieve required data and calculate it – we have had to communicate with the server (TCP protocol). Such operations of course are time consuming so I decided to use Parallel.Foreach.

See following code example:

15 public TimeSpan GetRecordedDuration(int recordingId)

16 {

17       TimeSpan duration = TimeSpan.Zero;

18       Parallel.ForEach(GetGroups(recordingId), group =>

19       {

20

21         var bookmarks = m_clientRecordingSys.GetGroupBookmarks(StreamsNames.VideoSourceName, group);

22         if (bookmarks.Count <= 1)

23         {

24             continue;

25         }

26         else

27       {

28       var startTime = TimeSpan.FromTicks(bookmarks[0].CreationTime);

29 var endTime   = TimeSpan.FromTicks(bookmarks[bookmarks.Count – 1].CreationTime);

30 duration += endTime – startTime;

31    }

32  }

33    );

34   return duration;

35

36 }

37

38 }

Using Data Parallelism sounds very nice and easy, but we have to be careful therefore it’s number of threads running simultaneously.

This line of code is not thread safe.
duration += endTime – startTime;
So we need to perform something like this:

lock(syncObj)

 {

      duration += endTime – startTime;

 }

Personally I didn’t like this synchronization lock that little bit miss the point of creating number of threads.

Instead of that I decided to use new feature that comes with v4.0 as well called,
Parallel LINQ (PLINQ)
See code example that uses PLINQ:

public TimeSpan GetRecordedDuration(int recordingId)

 {

     var durationTicks = GetGroups(recordingId)

                        .AsParallel().Sum(group =>

                        {

                            var bookmarks = this.m_clientRecordingSys.GetGroupBookmarks(StreamsNames.VideoSourceName

                                            , group);

                            if (bookmarks.Count <= 1)

                            {

                                return 0;

                            }

                            else

                            {

                                var startTime = bookmarks[0].CreationTime;

                                var endTime = bookmarks[bookmarks.Count – 1].CreationTime;

                                return (endTime – startTime);

                            }

                        });

 

      return TimeSpan.FromTicks(durationTicks);

    }

}

 

This code looks for me much cleaner and nice. This code works fine, giving good performance.
Hope this post will give you desire to try and enjoy new .Net framework as we do.

Categories: .Net Tags: ,
  1. January 4th, 2014 at 16:03 | #1

    Thanks for another excellent article. The place else may anybody get that kind of information in such an ideal manner of writing? I have a presentation next week, and I’m at the look for such information.

  1. No trackbacks yet.