Click here to Skip to main content
15,886,795 members
Articles / Programming Languages / F#

F# Asyncronous Programming

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
27 Jul 2010CPOL1 min read 18.1K   4   3
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.

F#
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)



Comments and Discussions

 
GeneralNice Article Pin
Simplicio Jison Jr3-Aug-10 3:35
Simplicio Jison Jr3-Aug-10 3:35 
GeneralMy vote of 2 Pin
dvhh27-Jul-10 21:20
dvhh27-Jul-10 21:20 
GeneralJust need formatting change! Pin
ThatsAlok26-Jul-10 23:46
ThatsAlok26-Jul-10 23:46 

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

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