This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #30 Parallelize execution of 1000 independent tasks
Launch the concurrent execution of the procedure f with parameter i from 1 to 1000.
Tasks are independent and f(i) doesn't return any value.
Tasks need not run all at the same time, so you may use a pool.
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- Java
- Kotlin
- Pascal
- Perl
- Python
- Ruby
- Rust
- Rust
auto futures = std::vector<std::future<void>>{};
for (auto i = 1; i <= 1000; ++i)
{
futures.emplace_back(std::async(f, i));
}
I used futures to join automatically threads through std::future destructor, one could use std::thread instead.
This code is written in c++17, in c++20, you could use ranges to make it smaller.
This code is written in c++17, in c++20, you could use ranges to make it smaller.
Parallel.For(1, 1001, f);
The Task Parallel Library handles partitioning and scheduling.
for (int i = 1; i <= 1000; i++) {
Isolate.spawn(f,i);
}
NOTE: The dart:isolate library is currently only supported by the Dart Native platform (not Web).
f = fn x -> x * :rand.uniform() end
tasks = Enum.map(1..1000, fn i ->
Task.async(fn ->
f.(i)
end)
end)
results = Task.yield_many(tasks, :infinity)
# Optional, if you care for the result
IO.inspect Enum.map(results, fn {_, {:ok, val}} -> val end)
async creates a parallel task.
yield_many awaits on many given tasks for a given time. By default it timeouts on 5 seconds, and timed-out tasks will keep running. Passing :infinite as the second argument disables the timeout and blocks the caller indefinitely.
yield_many always returns a list of pairs, with the task and the possible return value, either a tuple of :ok and the returned value, a tuple of :error and the reason for it, or nil if the task timed out.
yield_many awaits on many given tasks for a given time. By default it timeouts on 5 seconds, and timed-out tasks will keep running. Passing :infinite as the second argument disables the timeout and blocks the caller indefinitely.
yield_many always returns a list of pairs, with the task and the possible return value, either a tuple of :ok and the returned value, a tuple of :error and the reason for it, or nil if the task timed out.
type
TThreadF = class(TThread)
i: Integer;
constructor Create(const _i: Integer);
procedure Execute; override;
end;
constructor TThreadF.Create(const _i: Integer);
begin
i := _i;
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TThreadF.Execute;
begin
f(i);
end;
var i: Integer;
begin
for i := 1 to 1000 do begin TThreadF.Create(i);
ReadLn;
end.
No simple thread pooling in Pascal. Indy components have thread pooling classes.
pool = Pool()
for i in range(1, 1001):
pool.apply_async(f, [i])
let threads: Vec<_> = (0..1000).map(|i| {
thread::spawn(move || f(i))
}).collect();
for thread in threads {
thread.join();
}
If you don't join the threads, the program will just exit and the threads will be killed.
This would be better implemented with a thread pool but the standard library doesn't include one.
This would be better implemented with a thread pool but the standard library doesn't include one.
programming-idioms.org