-
Notifications
You must be signed in to change notification settings - Fork 0
/
PsThreading.Patterns.ps1
137 lines (122 loc) · 4.92 KB
/
PsThreading.Patterns.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
################################################################################
# Author : Antony Onipko
# Copyright : (c) 2016 Antony Onipko. All rights reserved.
################################################################################
# This work is licensed under the
# Creative Commons Attribution-ShareAlike 4.0 International License.
# To view a copy of this license, visit
# https://creativecommons.org/licenses/by-sa/4.0/
################################################################################
$PsThreading = [pscustomobject]@{
Parameter = [pscustomobject]@{}
Thread = [pscustomobject]@{
Consumer = {
Param($ThreadId, $WorkQueue, $Settings, $ResultSet)
$item = ""
while (!$Settings['ProducerIsDone'] -or $WorkQueue.Count -gt 0) {
if ($WorkQueue.TryDequeue([ref]$item)) {
#process item
$ResultSet.Add("Thread $ThreadId has processed '$item'")
} else {
Start-Sleep -Milliseconds 10
}
}
}
Producer = {
Param($ThreadId, $WorkQueue, $Settings)
1..$Settings['NumberToProduce'] | % {
$WorkQueue.Enqueue("item number $_")
}
$Settings['ProducerIsDone'] = $true
}
Worker = {
Param($ThreadId, $WorkQueue)
$item = ""
while ($WorkQueue.TryDequeue([ref]$item)) {
# process item
Write-Output "$ThreadId -> $item"
}
}
Writer = {
Param($ThreadId, $Settings, $ResultSet)
$item = ""
$sleepTime = $Settings['SleepTime']
$stream = [System.IO.File]::OpenWrite($Settings['OutputPath'])
$writer = New-Object System.IO.StreamWriter($stream)
$writer.WriteLine("$(Get-Date -Format "yyyyMMddHHmmss") -> Writer starting")
while ($Settings['WorkersRunning'] -gt 0 -or $ResultSet.Count -gt 0) {
if ($ResultSet.TryTake([ref]$item)) {
$writer.WriteLine($item)
} else {
Start-Sleep -Milliseconds $sleepTime
}
}
$writer.WriteLine("$(Get-Date -Format "yyyyMMddHHmmss") -> Writer id done.")
$writer.Flush()
$stream.Close()
}
}
Utility = [pscustomobject]@{}
}
################################################################################
$workerOnly = {
return @{
WorkQueue = New-Object System.Collections.Concurrent.ConcurrentQueue[object]
}
}
Add-Member -InputObject $PsThreading.Parameter `
-MemberType ScriptProperty `
-Name WorkerOnly `
-Value $workerOnly
$producerConsumer = {
$sets = New-Object 'System.Collections.Concurrent.ConcurrentDictionary`2[string,object]'
$sets['NumberToProduce'] = 10000
$sets['ProducerIsDone'] = $false
return @{
ResultSet = New-Object System.Collections.Concurrent.ConcurrentBag[object]
Settings = $sets
WorkQueue = New-Object System.Collections.Concurrent.ConcurrentQueue[object]
}
}
Add-Member -InputObject $PsThreading.Parameter `
-MemberType ScriptProperty `
-Name ProducerConsumer `
-Value $producerConsumer
$producerWorkerWriter = {
$sets = New-Object 'System.Collections.Concurrent.ConcurrentDictionary`2[string,object]'
$sets['OutputPath'] = (Join-Path (Get-Location) out.txt)
$sets['NumberToProduce'] = 10000
$sets['SleepTime'] = 10
$sets['ProducerIsDone'] = $false
$sets['WorkersRunning'] = 0
return @{
ResultSet = New-Object System.Collections.Concurrent.ConcurrentBag[object]
Settings = $sets
WorkQueue = New-Object System.Collections.Concurrent.ConcurrentQueue[object]
}
}
Add-Member -InputObject $PsThreading.Parameter `
-MemberType ScriptProperty `
-Name ProducerWorkerWriter `
-Value $producerWorkerWriter
################################################################################
$numCores = {
return Get-WmiObject Win32_Processor |
Measure-Object -Property NumberofCores -Sum |
select -ExpandProperty Sum
}
Add-Member -InputObject $PsThreading.Utility `
-MemberType ScriptProperty `
-Name CpuCores `
-Value $numCores
$numCPUs = {
return Get-WmiObject Win32_Processor |
Measure-Object -Property NumberOfLogicalProcessors -Sum |
select -ExpandProperty Sum
}
Add-Member -InputObject $PsThreading.Utility `
-MemberType ScriptProperty `
-Name LogicalCpus `
-Value $numCPUs
################################################################################
Export-ModuleMember -Variable PsThreading