Skip to content

Commit 629911e

Browse files
authored
ThreadStaticAttribute F# snippet (#7877)
1 parent 8ffd9cc commit 629911e

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="threadsafe2a.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// <Snippet1>
2+
open System
3+
open System.Threading
4+
5+
type Example() =
6+
[<ThreadStatic; DefaultValue>]
7+
static val mutable private previous : double
8+
9+
[<ThreadStatic; DefaultValue>]
10+
static val mutable private sum : double
11+
12+
[<ThreadStatic; DefaultValue>]
13+
static val mutable private calls : int
14+
15+
[<ThreadStatic; DefaultValue>]
16+
static val mutable private abnormal : bool
17+
18+
static let mutable totalNumbers = 0
19+
static let countdown = new CountdownEvent(1)
20+
static let lockObj = obj ()
21+
let rand = Random()
22+
23+
24+
member this.Execute() =
25+
for threads = 1 to 10 do
26+
let newThread = new Thread(ThreadStart this.GetRandomNumbers)
27+
countdown.AddCount()
28+
newThread.Name <- threads.ToString()
29+
newThread.Start()
30+
this.GetRandomNumbers()
31+
countdown.Wait()
32+
printfn $"{totalNumbers:N0} random numbers were generated."
33+
34+
member _.GetRandomNumbers() =
35+
let mutable i = 0
36+
while i < 2000000 do
37+
lock lockObj (fun () ->
38+
let result = rand.NextDouble()
39+
Example.calls <- Example.calls + 1
40+
Interlocked.Increment &totalNumbers |> ignore
41+
// We should never get the same random number twice.
42+
if result = Example.previous then
43+
Example.abnormal <- true
44+
i <- 2000001 // break
45+
else
46+
Example.previous <- result
47+
Example.sum <- Example.sum + result )
48+
i <- i + 1
49+
// get last result
50+
if Example.abnormal then
51+
printfn $"Result is {Example.previous} in {Thread.CurrentThread.Name}"
52+
53+
printfn $"Thread {Thread.CurrentThread.Name} finished random number generation."
54+
printfn $"Sum = {Example.sum:N4}, Mean = {Example.sum / float Example.calls:N4}, n = {Example.calls:N0}\n"
55+
countdown.Signal() |> ignore
56+
57+
let ex = Example()
58+
Thread.CurrentThread.Name <- "Main"
59+
ex.Execute()
60+
61+
// The example displays output similar to the following:
62+
// Thread 1 finished random number generation.
63+
// Sum = 1,000,556.7483, Mean = 0.5003, n = 2,000,000
64+
//
65+
// Thread 6 finished random number generation.
66+
// Sum = 999,704.3865, Mean = 0.4999, n = 2,000,000
67+
//
68+
// Thread 2 finished random number generation.
69+
// Sum = 999,680.8904, Mean = 0.4998, n = 2,000,000
70+
//
71+
// Thread 10 finished random number generation.
72+
// Sum = 999,437.5132, Mean = 0.4997, n = 2,000,000
73+
//
74+
// Thread 8 finished random number generation.
75+
// Sum = 1,000,663.7789, Mean = 0.5003, n = 2,000,000
76+
//
77+
// Thread 4 finished random number generation.
78+
// Sum = 999,379.5978, Mean = 0.4997, n = 2,000,000
79+
//
80+
// Thread 5 finished random number generation.
81+
// Sum = 1,000,011.0605, Mean = 0.5000, n = 2,000,000
82+
//
83+
// Thread 9 finished random number generation.
84+
// Sum = 1,000,637.4556, Mean = 0.5003, n = 2,000,000
85+
//
86+
// Thread Main finished random number generation.
87+
// Sum = 1,000,676.2381, Mean = 0.5003, n = 2,000,000
88+
//
89+
// Thread 3 finished random number generation.
90+
// Sum = 999,951.1025, Mean = 0.5000, n = 2,000,000
91+
//
92+
// Thread 7 finished random number generation.
93+
// Sum = 1,000,844.5217, Mean = 0.5004, n = 2,000,000
94+
//
95+
// 22,000,000 random numbers were generated.
96+
// </Snippet1>

xml/System/ThreadStaticAttribute.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
## Remarks
6464
A `static` field marked with <xref:System.ThreadStaticAttribute> is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.
6565
66-
Note that in addition to applying the <xref:System.ThreadStaticAttribute> attribute to a field, you must also define it as a `static` field (in C#) or a `Shared` field (in Visual Basic).
66+
Note that in addition to applying the <xref:System.ThreadStaticAttribute> attribute to a field, you must also define it as a `static` field (in C# or F#) or a `Shared` field (in Visual Basic).
6767
6868
> [!NOTE]
6969
> Do not specify initial values for fields marked with `ThreadStaticAttribute`, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to `null` if it is a reference type.
@@ -78,9 +78,10 @@
7878
The following example instantiates a random number generator, creates ten threads in addition to the main thread, and then generates two million random numbers in each thread. It uses the <xref:System.ThreadStaticAttribute> attribute to calculate the sum and the count of random numbers per thread. It also defines two additional per-thread fields, `previous` and `abnormal`, that allows it to detect corruption of the random number generator.
7979
8080
:::code language="csharp" source="~/snippets/csharp/System/ThreadStaticAttribute/Overview/threadsafe2a.cs" id="Snippet1":::
81+
:::code language="fsharp" source="~/snippets/fsharp/System/ThreadStaticAttribute/Overview/threadsafe2a.fs" id="Snippet1":::
8182
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.threadstaticattribute/vb/threadsafe2a.vb" id="Snippet1":::
8283
83-
The example uses the `lock` statement in C# and the `SyncLock` construct in Visual Basic to synchronize access to the random number generator. This prevents corruption of the random number generator, which typically results in its returning a value of zero for all subsequent calls.
84+
The example uses the `lock` statement in C#, the `lock` function in F#, and the `SyncLock` construct in Visual Basic to synchronize access to the random number generator. This prevents corruption of the random number generator, which typically results in its returning a value of zero for all subsequent calls.
8485
8586
The example also uses the <xref:System.Threading.CountdownEvent> class to ensure that each thread has finished generating random numbers before it displays the total number of calls. Otherwise, if the main thread completes execution before the additional threads that it spawns, it displays an inaccurate value for the total number of method calls.
8687

0 commit comments

Comments
 (0)