Skip to content

Dynamically created thread bug #21

@Denages

Description

@Denages

I want to create a thread dynamically, but if the time elapsed from the start is bigger than just-created thread interval the thread is invoked immediately.
@ivanseidel

Here is an example:

#include <Thread.h>
#include <ThreadController.h>

ThreadController controll = ThreadController();
Thread *selfCancellingThread = NULL;
Thread addingThread = Thread();

void selfCancellingCallback(){
  controll.remove(selfCancellingThread);
  delete selfCancellingThread;
  Serial.println("Thread was self-destroyed");
}

void addingCallback(){
  controll.remove(selfCancellingThread);
  delete selfCancellingThread;
  selfCancellingThread = new Thread(selfCancellingCallback, 2000);
  controll.add(selfCancellingThread);
  Serial.println("Thread was added");
}

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  addingThread.setInterval(5000);
  addingThread.onRun(addingCallback);
  controll.add(&addingThread);

}

void loop() {
  controll.run();
}

Result:
arduinothreadbug

If I increase selfCancellingThread interval to 6000, then it will be called after (6000-5000) = 1 sec at the first invocation:

...
selfCancellingThread = new Thread(selfCancellingCallback, 6000);
...

Result:
arduinothreadbug

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions