-1
void a(){
     delay(3000)
     # other statement
}

void b(){
     delay(3000)
     # other statement
}

Now I want to run these function in parallel but I when I call first function then it should cancel the delay time of other function and other functionality of function b and vice versa. My aim to run it in parallel.

  • 1
    What is `delay`? Are you running this on a system with threads or is it a microcontroller/Arduino type of thing? Also, please pick a language, either C++ or C. – David Grayson Mar 26 '22 at 03:18
  • yes i want to run it on aurdino . language is c++ – ZAIN DURANI Mar 26 '22 at 03:47
  • @DavidGrayson yes coding for some functionality in aurdino i am using c++. but i want to make delay but at same when user enter call function it should cancel first function delay and other statment – ZAIN DURANI Mar 26 '22 at 03:49
  • @BillLynch `std::thread` doesn't work on arduino. – Passer By Mar 26 '22 at 04:28
  • @PasserBy: At the time I dupped this, there was nothing about an Arduino in this question. – Bill Lynch Mar 26 '22 at 15:11
  • @BillLynch Ah I see, I thought that was weird. – Passer By Mar 26 '22 at 15:51
  • 2
    Ignore the unhelpful comments. Search for "blink with no delay". It's in the example sketches in the Arduino IDE the first one under "02 Digital". It won't tell you how to rewrite your app, but it will show you what you need to know. – aMike Mar 26 '22 at 15:51

1 Answers1

0

As a real Arduino does not run an operating system, you have to do some sort of multitasking by yourself. Refrain from writing blocking functions. Your function void a() should look similar to:

    void a() {
       static unsigned long lastrun;
       if (millis() - lastrun >= 3000) {
           lastrun=millis();
           // non-blocking code to be executed once every 3 sec
       }
    }

As Arduino is written in C++, feel free to create a timer class doing this for you (and avoid static or global variables) But that's probably beyond your question.

datafiddler
  • 1,755
  • 3
  • 17
  • 30