Logo

Programming-Idioms

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

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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()
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
List<Future<T>> a;
try (ExecutorService executorService = Executors.newFixedThreadPool(3)) {
    a = executorService.invokeAll(List.of(f1, f2, f3));
}

T a1 = a.get(0).get();
T a2 = a.get(1).get();
T a3 = a.get(2).get();
sysutils
  ThreadCount := 3;
  BeginThread(@f1,@a1);
  BeginThread(@f2,@a2);
  BeginThread(@f3,@a3);
  while ThreadCount > 0 do ;
from concurrent.futures import ThreadPoolExecutor
e = ThreadPoolExecutor()
futures = [e.submit(func) for func in (f1, f2, f3)]
a1, a2, a3 = [f.result() for f in futures]
def f1 = __method__
def f2 = __method__
def f3 = __method__

ractors = [Ractor.new{f1}, Ractor.new{f2}, Ractor.new{f3} ]
a1, a2, a3 = ractors.map(&:take)