Last active
July 9, 2019 00:15
-
-
Save todmephis/23ad2d595e1e5997cb78018c4bb4df45 to your computer and use it in GitHub Desktop.
Ejemplo 2 de hilos en C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <pthread.h> | |
typedef struct mamadas{ | |
int theadID; | |
char somerandombytes[1024]; | |
}SACA; | |
/*Otro ejemplo de hilos en C*/ | |
void* f_hilo(void* params){ | |
SACA *parametros=(SACA *)params; | |
//int *int_params=(int *)params; | |
printf("\n valor %d, cadena %s", parametros->theadID, parametros->somerandombytes); | |
//pthread_exit(NULL); Se elimina debido a: | |
/* If the | |
start_routine returns, the effect is as if there was an implicit call to pthread_exit() using | |
the return value of start_routine as the exit status.*/ | |
return NULL; | |
} | |
int main(int argc, char **argv){ | |
pthread_t *hilo; | |
int i, N=0; | |
SACA *j; | |
N = atoi(argv[1]); | |
hilo = (pthread_t *)malloc(N*sizeof(hilo)); | |
j = (SACA *)malloc(sizeof(j)*N); | |
for(i=0;i<N;i++){ | |
j[i].theadID=i; | |
strcpy(j[i].somerandombytes, "HOLA"); | |
pthread_create(&hilo[i], NULL, f_hilo, (void *) &j[i]); | |
//pthread_create(pthread_t *thread, const pthread_attr_t *attr, | |
//void *(*start_routine)(void *), void *arg) | |
} | |
for(i=0;i<N;i++){ | |
pthread_join(hilo[i], NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment