Archive 07/10/2020.

Plusieurs threads

emmo78

Quand j’essai ce code :

static void *thread_handlera(void *arg)
{
(void)arg;
puts(“Hello from threada”);
return NULL;
}

static void *thread_handlerb(void arg)
{
(void)arg;
puts(“Hello from threadb”);
return NULL;
}

int main(void) {
/
Start new threads here */
thread_create(stack,
sizeof(stack),
THREAD_PRIORITY_MAIN - 1,
0,
thread_handlera,
NULL,
“new threada”);
thread_create(stack,
sizeof(stack),
THREAD_PRIORITY_MAIN - 2,
0,
thread_handlerb,
NULL,
“new threadb”);
…}
J’obtiens :
main(): This is RIOT! (Version: 2020.01)
Hello from threada
Hello from threadb

Mais si :
pour threada : THREAD_PRIORITY_MAIN + 1,
pour threadb : THREAD_PRIORITY_MAIN + 2,
Alors :
ps
pid | name | state Q | pri | stack ( used) | base addr | current
- | isr_stack | - - | - | 8192 ( -1) | 0x56582380 | 0x56582380
1 | idle | pending Q | 15 | 8192 ( 420) | 0x565800a0 | 0x56581f10
2 | main | running Q | 7 | 12288 ( 3136) | 0x5657d0a0 | 0x5657ff10
4 | new threadb | pending Q | 9 | 12288 (12284) | 0x5657a0a0 | 0x5657cf10
4 | new threadb | pending Q | 9 | 12288 (12284) | 0x5657a0a0 | 0x5657cf10
| SUM | | | 53248 (28124)
où est mon threada ?
Merci de votre réponse, cordialement

aabadie2

Il y a semble-t-il une erreur dans votre code: les 2 threads sont créés avec avec la même stack mémoire (le tableau statique appelé stack), c’est pour cette raison que threadb apparait 2 fois.
Il faut allouser une stack pour chaque thread.

Ensuite tout dépend de la fin de votre main. S’il se termine par le lancement du shell alors threada, ayant une priorité plus faible que le thread main, ne sera jamais schédulé car bloqué par l’exécution du thread main. Et il apparaitra alors dans la sortie de la commande ps dans l’état pending.