5 years, 6 months ago.

Threadの一時停止と再開の方法がわかりません。

Threadの一時停止と再開の方法がわかりません。osStatusはosErrorParameterを返します。どうすればThreadを再開できますか?

sample

#include "mbed.h"

Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;

void printOSStatus(osStatus status)
{
    if (status == osOK)
    {
        printf("osOK\n");
    }
    else if (status == osErrorValue)
    {
        printf("osErrorValue\n");
    }
    else if (status == osErrorResource)
    {
        printf("osErrorResource\n");
    }
    else if (status == osErrorTimeoutResource)
    {
        printf("osErrorTimeoutResource\n");
    }
    else if (status == osErrorNoMemory)
    {
        printf("osErrorNoMemory\n");
    }
    else if (status == osErrorISR)
    {
        printf("osErrorISR\n");
    }
    else if (status == osErrorISRRecursive)
    {
        printf("osErrorISRRecrusive\n");
    }
    else if (status == osErrorPriority)
    {
        printf("osErrorPriority\n");
    }
    else if (status == osErrorParameter)
    {
        printf("osErrorParameter\n");
    }
    else if (status == osErrorOS)
    {
        printf("osErrorOS\n");
    }
    else if (status == osEventSignal)
    {
        printf("osEventSignal\n");
    }
    else if (status == osEventMessage)
    {
        printf("osEventMessage\n");
    }
    else if (status == osEventMail)
    {
        printf("osEventMail\n");
    }
    else if (status == osEventTimeout)
    {
        printf("osEventTimeout\n");
    }
    else
    {
        printf("else\n");
    }
}

void blink(DigitalOut *led) {
    while (running) {
        *led = !*led;
        wait(1);
    }
}

// Spawns a thread to run blink for 5 seconds
int main() {
    thread.start(callback(blink, &led1));
    wait(5);
    running = false;
    thread.join();

  running = true;
    osStatus status = thread.start(callback(blink, &led1));
    printOSStatus(status);
    wait(5);
}

1 Answer

5 years, 6 months ago.

Hi there,

when you set the "running" variable to false then the while loop of the "blink" function get out of scope and the thread is terminated. You can't start it again, the thread can be started only once. So you need create a new thread or change the code inside the "blink" function to endless loop. Something like that…

Thread with button

#include "mbed.h" //(5.12.2)
 
Thread thread;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
InterruptIn mybutton(USER_BUTTON);

volatile bool running = true;

void pressed()
{
    running =! running;
}

void blink(DigitalOut *led) {
    while (1) {
        while(running) { 
            *led = !*led;
            printf("Blink\n");
            ThisThread::sleep_for(1000);
        }
        printf("waiting\n");
        ThisThread::sleep_for(1000);
    }
}
 
int main() {
    printf("Thread example startting...\n");
    thread.start(callback(blink, &led1));
    mybutton.rise(callback(pressed));

    wait(0.5);
    
    while (1) {
        led2 = !led2;
        wait(1);
    }
}

https://os.mbed.com/docs/mbed-os/v5.12/apis/rtos.html

I don't speak Japanese but google translate helped me :)

Best regards

J.