Simple thread

Hi friends,

I am tryng to do a very simple thread application, with will configure and blink a led, but it isn’t working. Can you see if I made some mistake? This code on *run works ok if I put it on main(void). I am very newbie on Riot :slight_smile: Tahnks.

The code:

void *run(void *parameter) { gpio_init_out(21, GPIO_NOPULL); gpio_init_out(22, GPIO_NOPULL); gpio_init_out(23, GPIO_NOPULL); for (int c = 0; c < 6; c++) { LED_GREEN_TOGGLE; hwtimer_wait(1000000); } return NULL; } int main(void) { pthread_t th_id; pthread_attr_t th_attr; size_t arg = 6; pthread_attr_init(&th_attr); pthread_create(&th_id, &th_attr, run, (void *) arg); while (1){} return 0; }

You can't do this in RIOT. The thread with the highest priority is always running, so the other thread will not start...

btw. this chunk of code is bad on linux too, because it will simply burn your cpu...

better use thread_{sleep,wait} or pthread_join.

you can take a look at the pthread tests in the test folder. for a simple test with such a simple logic I would not use the pthread abstraction...you can just use riot threads (core/threads).

Good luck, Christian

Hi Marcio,

welcome to RIOT! With the following modifications your code runs for me. If there are open questions don’t hesitate to ask here.

Best, Thomas


#include "board.h"
#include "pthread.h"
#include "hwtimer.h"

void *run(void *parameter) {
for (int c = 0; c < 6; c++) {
LED_GREEN_TOGGLE;
hwtimer_wait(1000000);
}
return NULL;
}

int main(void)
{
pthread_t th_id;
pthread_attr_t th_attr;
size_t arg = 6;
pthread_attr_init(&th_attr);
pthread_create(&th_id, &th_attr, run, (void *) arg);
/* wait for run() */
pthread_join(th_id, NULL);
/* signal termination of run() */
LED_RED_TOGGLE;
return 0;
}