#include h> #include h> #include h> #define

Anuncio
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define THREADS_NUM 4
void * imprimirNumeroThread(void* parametros);
int main (void){
pthread_t threads[THREADS_NUM];
int threads_params[THREADS_NUM];
int *threads_results[THREADS_NUM];
for (int i = 0; i < THREADS_NUM; i++) {
threads_params[i] = i+1;
pthread_create(&(threads[i]), NULL, imprimirNumeroThread, (void*) &(threads_params[i]));
}
for (int i = 0; i <THREADS_NUM; i++) {
pthread_join(threads[i], (void**)&(threads_results[i]));
int numThread = *(threads_results[i]);
printf("A thread %d terminou sua tarefa\n", numThread);
}
}
void * imprimirNumeroThread(void* parametros) {
int *numeroThread = (int*)malloc(sizeof(int));
*numeroThread = *(int *)parametros;
printf("Thread %d em execução\n", *numeroThread);
pthread_exit(numeroThread);
}
Descargar