-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstThread.c
More file actions
50 lines (38 loc) · 1.06 KB
/
firstThread.c
File metadata and controls
50 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define pprintf(f_, ...) printf(("\033[38;5;%um"f_"\e[0m"),30+(unsigned int)pthread_self()%255,##__VA_ARGS__)
void* myFunction(void*);
int main(){
/* Paso 1: Creación del hilo */
pthread_t* firstThread;
int x;
int f;
printf("Numero de hilos : ");
scanf("%d", &x);
firstThread = malloc( sizeof(pthread_t) * x); //
for (int i = 0; i < x; ++i) {
f = pthread_create(&firstThread[i], NULL, myFunction, &firstThread[i]);
pprintf("El padre creo al hilo %p \n", &firstThread[i]);
if(f == -1){
printf("Error al crear el hilo");
exit(1);
}
}
void * hiloid; //
for (int i = 0; i < x; ++i) {
pthread_join(firstThread[i],&hiloid);
pprintf("HIJO:%ld\n",*(pthread_t*)hiloid); //
//free(&firstThread[i]);
}
return 0;
}
void* myFunction(void* a){
pthread_t* b = (pthread_t *) a;
pprintf("Hola Mundo desde el hilo %p\n", b);
//pthread_exit(NULL);
pthread_t* retval = malloc(sizeof(pthread_t)); //
*retval = pthread_self(); //
pprintf("Mi hilo:%ld\n",*retval); //
return (void *) retval; //
}