Bonjour,

Je débute en threads C, et j'ai un petit problème d'annulation de threads.
Code C : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
 
void * my_routine(void *arg) {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d\n", i);
  }
  return NULL;
}
 
int main(int argc, char *argv[]) {
  pthread_t thread;
  int ret = pthread_create(&thread, NULL, my_routine, NULL);
  usleep(20);
  pthread_cancel(thread);
  pthread_join(thread, NULL);
  return 0;
}

De temps en temps, le printf n'affiche pas la bonne valeur au moment de l'annulation :
Comment l'éviter ?

pthread_cancel serait-il l'équivalent de Thread.stop() en Java : il ne faut jamais l'utiliser ?