arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.23k stars 224 forks source link

A quick question (disable, delete task from callback) #70

Closed mm108 closed 5 years ago

mm108 commented 5 years ago

Pretty cool library, thanks! A simple question - is it ok if I disable and delete a task from it's callback? Let me explain - there is a task that keeps running until a certain condition is met. The callback of the task keeps checking for the condition. Once the condition is met, the task is disabled and deleted ( from the same task's callback ). Is this ok? The reason I ask is because I am deleting the task from within it's own callback. Just wondering if that's bad or would that cause any weird behavior or some unexpected results. It seems to work but I thought I'd ask. Thanks a lot.

arkhipenko commented 5 years ago

It is ok for sure to delete a task from its OnDisable method - I tested that.

As far as Callback is concerned, should be ok as long as there is no follow up references, like OnDisable called right after. Generally it is a good practice to delete and return immediately.

Please do test!

On Dec 24, 2018 8:35 PM, RW notifications@github.com wrote:

Pretty cool library, thanks! A simple question - is it ok if I disable and delete a task from it's callback? Let me explain - there is a task that keeps running until a certain condition is met. The callback of the task keeps checking for the condition. Once the condition is met, the task is disabled and deleted ( from the same task's callback ). Is this ok? The reason I ask is because I am deleting the task from within it's own callback. Just wondering if that's bad or would that cause any weird behavior or some unexpected results. It seems to work but I thought I'd ask. Thanks a lot.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/arkhipenko/TaskScheduler/issues/70, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AATGTb-WyfEA37Q6pvOWSC1hebQjCKzwks5u8YDLgaJpZM4ZgvI6.

mm108 commented 5 years ago

Perfect, thanks! I just tested it a few more times and it worked reliably every time. Love it's configurability too!

Cheers

arkhipenko commented 5 years ago

A word of caution: apparently the ability to delete "itself" in the OnDisable method depends on the toolchain. On STM32F1 this does not work, while on Arduino and ESP8266 it does. Go figure!

GitMoDu commented 5 years ago

That's very odd. I'll try it on STM32F1.

arkhipenko commented 5 years ago

That's very odd. I'll try it on STM32F1.

Try running example 19 (minus the FreeMem() ) on STM32F1 (from the master branch - testing is now different). In my case, the Tasks are not properly deleted (the total # of tasks never goes to 0 reliably). In another case the sketch hangs in the OnDelete() method right at delete t; line with the blue LED blinking rapidly (as if a null pointer reference threw it into the bootloader?). I could not find an explanation of what a fast blinking blue LED means on Mini...

GitMoDu commented 5 years ago

Fast blinking blue LED means seg-fault a.k.a. null pointer exception.