BUT2/SCR/TP06/ex1.c

31 lines
642 B
C
Raw Normal View History

2023-10-12 16:39:49 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#define NUM_THREADS 16
void *thread(void *thread_id) {
int id = *((int *) thread_id);
printf("Hello from thread %d\n", id);
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
int tabid[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++){
tabid[i] = i+1;
}
for (int i = 0; i < NUM_THREADS; i++){
assert( pthread_create(&threads[i], NULL, thread, &tabid[i]) == 0);
}
for (int i = 0; i < NUM_THREADS; i++){
assert( pthread_join(threads[i], NULL) == 0);
}
return EXIT_SUCCESS;
}