Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: TeensyThreads Yield broken for 4.x?

  1. #1
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    819

    TeensyThreads Yield broken for 4.x?

    Possibly first mentioned here.
    Using Teensyduino 1.54 and Teensy 4.1(though it should be the same for any 4.x)
    TeensyThreads Yield does not seem to be working correctly, I started trying to use it on a project and I noticed that I wasn't getting the results I would expect so I did a minimal example that should just increment a value every millisecond and then print that value every second in the main loop. I would expect this value to be at or around 1000 by the time it gets printed, but this is not the case, I also ran the Tests sketch from TeensyThreads and get much of the same results as what AcidZero got in the other thread, besides the addition of Test delay yield failing.

    Results from Minimum Sketch:
    Code:
    Loop: 119937000    Thread: 91     
    Loop: 119936999    Thread: 91     
    Loop: 119936993    Thread: 91     
    Loop: 119936998    Thread: 91
    Here's the minimum Sketch that does not give the correct results(you can also replace threads.delay(1) with threads.yield() and see that it outputs the same exact results):
    Code:
    #include <TeensyThreads.h>
    
    volatile uint32_t threadLooped = 0;
    volatile uint32_t thread_poll_id = 0;
    void thread_poll() {
      while(1) {
          threadLooped++;
          threads.delay(1);
    //      threads.yield();
      }
    }
    
    void setup() {
      while(!Serial);
      if(CrashReport){
        Serial.println(CrashReport);
      }
    
      thread_poll_id = threads.addThread(thread_poll);
    }
    
    elapsedMillis seconds = 0;
    volatile uint32_t loopCounter = 0;
    
    void loop() {
      while(1){
        loopCounter++;
        if(seconds >= 1000){
          seconds -= 1000;
          
          Serial.printf("Loop: %d    Thread: %d     \n", loopCounter, threadLooped);
      
          loopCounter = 0;
          threadLooped = 0;
        }
      }
    }
    My results from TeensyThread Tests sketch:
    Code:
    CPU speed consistency ***FAIL***
    Test thread start OK
    Test thread run state OK
    Test thread return OK
    Test thread speed ***FAIL***
    Speed no threads: 85705208
    Speed 1 thread: 60613234
    Ratio: 0.71
    Test set time slice OK
    Speed default ticks: 60613248
    Speed 200 ticks: 114710017
    Expected: 115453808.00
    Ratio with expected: 0.99
    Test delay yield ***FAIL***
    Yield wait ratio: 1.40
    Test thread end state OK
    Test thread reinitialize OK
    Test stack usage OK
    Test thread suspend OK
    Test thread restart OK
    Test thread stop OK
    Test thread start OK
    Test thread wait OK
    Test thread wait time OK
    Test thread kill OK
    Test std::thread scope OK
    Test basic lock OK
    Test basic unlock OK
    Test mutex lock state OK
    Test mutex lock thread OK
    Test mutex unlock OK
    Test fast locks OK
    24587500 25000000 21827474
    Test std::mutex lock OK
    Test std::mutex unlock OK
    Test Grab init OK
    Test Grab set OK
    Test Grab lock OK
    Test thread stack overflow OK
    Test infinite loop (will not end)
    0: 0 sec 75204080 587109984 17591694
    1: 5 sec 75204080 836691035 17591694
    2: 10 sec 75204080 1087371562 17591694
    3: 15 sec 75204080 1336952613 17591694
    4: 20 sec 75204080 1587633140 17591694
    5: 25 sec 75204080 1837214191 17591694
    6: 30 sec 75204080 2087894718 17591694
    7: 35 sec 75204080 -1957491527 17591694
    8: 40 sec 75204080 -1706811000 17591694
    9: 45 sec 75204080 -1457229949 17591694
    10: 50 sec 75204080 -1206549422 17591694
    11: 55 sec 75204080 -956968371 17591694
    12: 60 sec 75204080 -706287844 17591694

  2. #2
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    819
    I now see that in my example sketch I had to do threads.setSliceMicros(10); in order to have such a short threads.delay, though I am curious on why Test delay yield failed in the TeensyThreads Tests sketch, that lead me to believe there was nothing wrong on my part.

    You can disregard this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •