SCR/SCR3.1/TP6/ex1.c

31 lines
591 B
C
Raw Normal View History

2023-10-12 16:07:28 +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(int argc, char *argv[])
{
int th[NUM_THREADS];
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++){
th[i] = i;
assert( pthread_create(&threads[i], NULL, thread, &th[i]) == 0);
}
for (int i = 0; i < NUM_THREADS; i++)
assert( pthread_join(threads[i], NULL) == 0);
return EXIT_SUCCESS;
}