[C con Clase] Problemas con hilos

Armando B. VERA rdlmat en gmail.com
Mie Ago 1 01:07:06 CEST 2012


A ver si alguien me puede ayudar. Estoy probando este código pero no puedo
compilar.

He marcado la linea de error. La salida al compilar es:

armando en abvera:~/PROGRAMACION/SOM$ gcc -o hilos hilos.c -pthread

hilos.c: In function ‘main’:

hilos.c:21: error: expected expression before ‘%’ token

hilos.c:21: error: too few arguments to function ‘pthread_create’

armando en abvera:~/PROGRAMACION/SOM$

La declaracion de la funcion pthread_create que se encuentra en pthread.h y
tiene la siguiente forma:

extern int pthread_create (pthread_t *__restrict __newthread,

__const pthread_attr_t *__restrict __attr,

void *(*__start_routine) (void *),

void *__restrict __arg) __THROW __nonnull ((1, 3));

El código del ejemplo es:

------------------------------------------------------------------------

/* **** Prueba de hilos **** */

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#define N_HILOS 10

void *imprimir_hola_mundo(void *tid) {

printf("Hola mundo. Saludos del hilo %d0",tid);

pthread_exit(NULL);

}

int main(int argc, char *argv[]) {

pthread_t hilos[N_HILOS];

int estado;

int i;

for(i=0;i<N_HILOS;i++) {

printf("Aquí main. Creando hilo %d0", i);

 estado=pthread_create(%hilos[i],NULL, imprimir_hola_mundo(void *)i); //
LINEA DE ERROR

if(estado!=0) {

estado=("Ups! pthread_create devolvio el código de error %d0", estado);

exit(-1);

}

}

exit((int)NULL);

}

----------------------------------------------------------------------
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20120731/62c4b1d0/attachment.html>


Más información sobre la lista de distribución Cconclase