Click here to Skip to main content
Licence CPOL
First Posted 27 Jul 2010
Views 6,697
Bookmarked 2 times

F# Asyncronous Programming

By | 27 Jul 2010 | Article
Async programming in functional languages such as F# is such a breeze....

Introduction

F# is a strongly-typed language like C#, but with a lightweight syntax often seen in a dynamic language like Python. This gives your F# programs a lightweight, math-like feel.

Basically, we are not going to get faster CPUs in the future. What we are going to get is more CPUs. The challenge for programmers is then to write software which can take advantage of the extra CPUs... It seems that every tutorial on concurrency has to cite The Free Lunch is Over: A Fundamental Turn Toward Concurrency in Software by Herb Sutter

Background

There are many other places where you can find useful information about F#. First of all, there is an official F# web site, where you can find the language specification, documentation and other useful resources. There are also two books written about F# (one already published) Expert F# (by Don Syme, Adam Granicz and Antonio Cisternino) and Foundations of F# (by Robert Pickering).

Using the Code

The code of this tutorial shows how to achieve that in F#, and how easy it is.

open System

type Class1 (ind:int) = 
    let randomnum = new Random()
    member this.Func1 () = 
        System.Threading.Thread.Sleep(randomnum.Next(5)*1000)
        printfn "this is the thread name %A" ind      

type public SampleAsync () =

    let listofClass1 = 
        [for i in 1 .. 10 -> new Class1(i)]

    member public this.fetchAsync(cls:Class1) = 
        async{
            try
                cls.Func1()
            with
                | ex -> printfn "error: %A" ex.Message
        }

    member public this.runAll() =
        listofClass1
        |> Seq.map this.fetchAsync
        |> Async.Parallel 
        |> Async.RunSynchronously
        |> ignore

let sampleasync = new SampleAsync()
sampleasync.runAll()

Here, I am creating a simple Class that has a member which generates a random sleep on each class.

Points of Interest

Does F# kill C++?

At least to some extent F# programs could run at par with C++ ones. Below is the link to Flying Frog blog where people performed comparisons for ray tracing:

http://fsharpnews.blogspot.com/2010/03/f-vs-unmanaged-c-for-parallel-numerics.html

"We obtained a surprising performance result when comparing optimized parallel ray tracers written in F# and C++ recently. The following two programs render the same highly complex scenes containing over a million objects. Surprisingly, the 136-line managed F# program runs slightly faster at 17s than the 168-line unmanaged C++ which takes 18s."

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

winston gubantes

Software Developer (Senior)
Accelerate Desk Business Solutions
Philippines Philippines

Member

I started development Since Turbo C++, Started Windows programming with VB 6.0,Database Programming with Oracle, MS SQL Server and MySQL and Web programming with ASP.Net and PHP. Now I build Windows Forms and Web applications using Microsoft.NET. specializing on multi-threading and parallel programming

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice Article PinmemberSimplicio Jison3:35 3 Aug '10  
GeneralMy vote of 2 Pinmemberdvhh21:20 27 Jul '10  
GeneralJust need formatting change! Pinmember ThatsAlok 23:46 26 Jul '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 27 Jul 2010
Article Copyright 2010 by winston gubantes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid