next up previous contents
Next: Terminaison d'un thread Up: Implantation Previous: Implantation   Contents

Création d'un thread

La mise en place d'un thread se fait par la commande :

thr_create(stack_addr, stack_size, start_routine, arg, flags, thread_id)

Les flags passés à la commande détermine son mode de fonctionnement :

[linewidth=1pt,fillstyle=solid,shadow=true](-0.5,0)(17.5,-19)

Ex : création de 10 threads


#include <thread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <string.h>

#define NTHREADS 10

void * athread(void * arg)
{
   printf("thread ID: %2d, arg: %2d\n",thr_self(),(int)arg);
   return(0); 
} 

void main(int argc, char * argv[])
{ 
   int i,n; 
   thread_t
   list[NTHREADS];

   for (i=0;i<NTHREADS;++i) {
      n=thr_create(NULL,0,athread,i,0,&list[i]);

      if (n) {
         fprintf(stderr,"thr_create: %s\n",strerror(n));
         exit(1); 
      } 
   }

   for(i=0;i<NTHREADS;++i) { 
      if (n=thr_join(list[i],NULL,NULL)) {
         fprintf(stderr,"thr_create: %s\n",strerror(n));
         exit(1); 
      }
   }
   return(0); 
}


next up previous contents
Next: Terminaison d'un thread Up: Implantation Previous: Implantation   Contents
Arnaud Revel
2001-11-26