c-uebungen/Assignment_029/solution/threads.c

34 lines
697 B
C
Raw Permalink Normal View History

2023-05-21 21:10:46 +02:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 3
#define TASK_SIZE 10
void* task(void* arg) {
for (int i = 1; i <= TASK_SIZE; i++) {
printf("%d ", i);
usleep(10);
}
printf("\n");
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
int thread_create_result = pthread_create(&threads[i], NULL, task, NULL);
if (thread_create_result != 0) {
printf("Fehler beim Erstellen des Threads %d\n", i);
return 1;
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}