Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Pascal

Idiom #352 Parallelize 3 computations

Execute the functions f1, f2, f3 concurrently, and assign their respective return values to a1, a2, a3.

sysutils
  ThreadCount := 3;
  BeginThread(@f1,@a1);
  BeginThread(@f2,@a2);
  BeginThread(@f3,@a3);
  while ThreadCount > 0 do ;

f1, f2 and f3 must decrement the global ThreadCount variable.
import "sync"
var wg sync.WaitGroup
wg.Add(3)
go func() {
	a1 = f1()
	wg.Done()
}()
go func() {
	a2 = f2()
	wg.Done()
}()
go func() {
	a3 = f3()
	wg.Done()
}()
wg.Wait()

3 new goroutines

New implementation...