#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid){
long tid;
tid = *(long*)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]){
pthread_t threads[NUM_THREADS];
int rc;
long t;
for (t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, &t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
Para la implementación de este sencillo ejemplo no tenemos que tener muchas cosas en cuenta, únicamente tener cuidado con el paso de parámetros que referencian a bloques de memoria. En nuestro caso, en primer lugar le pasamos como primer parámetro el thread que vamos a construir, acto seguido la estructura del thread, como tercer parámetro el manejador o la función que vayamos a utilizar y por último el argumento para nuestra función.
No hay comentarios:
Publicar un comentario