0

I am using an Arduino due and have multiple schedules running at the same time. I am using this Library.

I have a function that 3 different loops use. Two of the loops are driven by user interaction so timing is not possible. The function does reset global variables and I would have no issue with the variables being reset multiple times as it wouldn't cause any problems for me.

What I was wondering, on the very slim chance that two loops called the function at the same time, is it possible for the loops to modify the same variable at identical times?

My understanding of the scheduler would lead me to believe that the scheduler will only yield to another loop after it has completed a line of code and thus the to instructions to reset the global variable could not happen at the very same time, just extremely quickly one after the other.

Would this be a correct assumption?

0

From the [Arduino Reference][1] pages:

This is a cooperative scheduler in that the CPU switches from one task to another. The library includes methods for passing control between tasks.

[emphasis mine]

Which means that your code decides when it yields control to another scheduled loop (by calling yield(). At a quick glance, it appears that it also yields by returning. So yes, your assumption is correct.

  • Awesome that will work, I just wanted to ensure they ran 1 by 1 so that no corruption of global data could occur. Great news, thanks JRobert – user1649972 Mar 16 '17 at 13:51
0

It is a simple schedular. It only changes to another task when yield() is called. The delay() function uses yield() and a lot of code and libraries use delay().
That means that if you code does not contain a delay() then you need to call delay() or yield() now and then, or other tasks will never execute.
It is probably safe to use the same function, but every time a delay() is used, an other task could run or another task will continue from there.

  • Awesome, thanks for that Jot looks like It will work for what I need. – user1649972 Mar 16 '17 at 13:53
  • I'm testing that Scheduler library at the moment. It is very simple, but useful. The stack for the new tasks are allocated in the heap. The original Arduino loop() function uses the normal stack. It might therefor be better to do big things like Ethernet in the Arduino loop() and do smaller things in the created tasks. – Jot Mar 17 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.