Skip to content

Dynamically created thread bug #21

Closed

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

Activity

  1. added a commit that references this issue on May 15, 2017
    55b9855
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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