No. But even if it were you can't use thread static variables with async workflows because you have no guarantee what thread your code is going to execute on. Your options are

  1. Pass the Random object as a parameter to your async computation (and then be careful not to use it with an async operation that spawns threads).
  2. Synchronize access to a shared Random object.
  3. Use or write a thread-safe random number generator.
By on 1/19/2009 9:46 PM ()

I don't think the original poster cares which thread gets which rng, just wants to ensure that no rng object is called concurrently from multiple threads, and trying to avoid the syntactic overhead of a module/class to use ThreadStatic.

Here is some code to mull over.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type Threadsafe<'a when 'a : (new : unit -> 'a)>() =
    [<System.ThreadStatic>]
    [<DefaultValue(false)>]
    static val mutable x : 'a
    member this.Get() : 'a=
        if Threadsafe.x = Unchecked.defaultof<'a> then 
            Threadsafe.x <- new 'a()
        Threadsafe.x

type MyThingy() =
    static let mutable x = 0
    let me = System.Threading.Interlocked.Increment(&x)
    member this.Which() = me

let thingy = new Threadsafe<MyThingy>()    
let ptasks = seq { for i in  0 .. 5 -> async { return thingy.Get().Which() } }
let results = ptasks |> Async.Parallel |> Async.Run
printfn "%A" results
By on 1/19/2009 11:14 PM ()

That is exactly what I was looking for as indeed, I don't care which random number generator gets used by which thread.

Cheers, J

By on 1/23/2009 8:52 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper