Search notes:

Powershell command noun: threadJob

The PowerShell command noun threadJob is available with Powershell 7.

Collecting output produced by threads

$threads = @()
foreach ($t in 'seven', 'eight-three', 'three', 'forty-eight', 'nine', 'one-hundred', 'twelve') {

   $threads += start-threadJob {
      param($arg)
      start-sleep (get-random -maximum 10)
      return "$arg has $($arg.length) characters"
   } -argumentList $t
}

#
# Wait until threads are finished
#
$null = wait-job -job $threads

foreach ($thread in $threads) {
   receive-job $thread
}

Starting jobs with and without a mutex

The following example tries to demonstrate how using a System.Threading.Mutex object prevents race conditions.
Change the value of the variable $useMutex to either use or not use the mutex:
$shared = @{accu = 0}

$nofThreads = 10
$threads    = @()

for ($t = 0; $t -lt $nofThreads; $t++) {

   $threads += start-threadJob {

      $useMutex = $false   ###  Change here!  ###

      if ($useMutex) {
         [bool] $newMutex = $false
          $mtx = new-object System.Threading.Mutex($true, 'tq84-mtx', [ref] $newMutex)
      }

      for ($i = 0; $i -lt 10 ; $i++) {

          $shared = $using:shared

          if ($useMutex) {
             $mtx.WaitOne()
          }
          $accu_ = $shared.accu
          $accu_ = $accu_ + 1
          start-sleep -milliseconds 1  # Give race condition more chance to occur
          $shared.accu = $accu_

          if ($useMutex) {
             $mtx.RelaseMutex()
          }

          $cur ++
      }
   }
}

$t = 0
while ($t -lt $nofThreads) {
   if ($threads[$t].state -eq 'Completed') {
      $t++
   }
   else {
      start-sleep 1
   }
}

write-host "accu = $($shared.accu)"

See also

Powershell command nouns

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:51 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(51): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(66): id_of(Object(PDO), 'uri', '/notes/Windows/...') #2 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1758201036, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #3 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/command-inventory/noun/threadJob/index(113): insert_webrequest() #4 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 51