guild icon
Toit
#Waiting for a list of tasks
Thread channel in help
addshore
addshore 02/26/2025 03:12 PM
Im trying sometihng like

tasks := [] tasks.add (task (:: log.info "foo") tasks.add (task (:: do.something "else") tasks.do: |t| t.wait log.info "all done"

But I believe this isn't doing what I'd expect.
Is there a way to wait for a collection of actions such as tasks in this way?
bitphlipphar
bitphlipphar 02/26/2025 03:18 PM
Yes!
bitphlipphar
bitphlipphar 02/26/2025 03:19 PM
You can run a set of lambdas as a group individual tasks and wait for them to complete.
bitphlipphar
bitphlipphar 02/26/2025 03:20 PM
You can find some example usages in the tests: https://github.com/toitlang/toit/blob/master/tests/task-group-test.toit
💟1
bitphlipphar
bitphlipphar 02/26/2025 03:22 PM
It is a bit tricky to write this correctly, so I recommend trying to map your problem to a call to Task.group.
bitphlipphar
bitphlipphar 02/26/2025 03:24 PM
The alternative is probably creating a monitor.Semaphore and down it tasks.size times and up it in finally clauses in the individual task lambdas.
bitphlipphar
bitphlipphar 02/26/2025 06:03 PM
Here is another example that shows the simplifications achieved when starting to use Task.group in Jaguar: https://github.com/toitlang/jaguar/commit/0170761a87c43ad025ddbec0e77b36ff66d9f31f.(edited)
8 messages in total