guild icon
Toit
#Stop a blocking sleep
Thread channel in help
pkarsy
pkarsy 08/04/2025 06:15 PM
Hi, lets say we have this task

task::
pin := gpio.Pin 2 --output
while true:
pin.set 1
sleep --ms=2000
pin.set 0
sleep --ms=2000

After a button press(another task or the same task) we need to respond immediatelly, so the sleep here is inapropriate. I have tried with "monitor" Mutex await try-await but I cannot do it. Ok there is the possibility of a tight loop with "sleep --ms=50" but I have hoped for something more elegant
floitsch
floitsch 08/04/2025 06:17 PM
floitsch
floitsch 08/04/2025 06:18 PM
You can then have a sleep in one lambda and another lambda to listen to a channel (or so)
pkarsy
pkarsy 08/04/2025 06:19 PM
So, it is the equivalent of golang "select" ?
floitsch
floitsch 08/04/2025 06:21 PM
Close enough. You tell the group how many lambdas need to complete before it kills the others.
So it's more flexible.
On the other hand, it's probably more expensive than a select in go
pkarsy
pkarsy 08/04/2025 06:24 PM
As you said the other lamdas are killed. This is good, but are there any resources left ? (memory leak) I dont know the internals of toit VM so I have to ask
floitsch
floitsch 08/04/2025 06:25 PM
Toit has a GC.
That said: if you know of resources that should be closed, do that in a finally. Those are still executed; even when the task is killed.
(edited)
pkarsy
pkarsy 08/04/2025 06:32 PM
Thank you very much ! I will try this at night.
👍1
8 messages in total