Skip to content
Snippets Groups Projects

Übung2

Open omah03 requested to merge omah03/alp4-repo-template:Übung2 into main
1 file
+ 32
0
Compare changes
  • Side-by-side
  • Inline
threads.c 0 → 100644
+ 32
0
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
//Each thread prints one prime number
//ID of thread shown by pthread_self
int primes[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
void* routine(void* arg) {
int index = *(int*)arg;
printf("Thread %ld printed prime %d\n",pthread_self(),primes[index]);
free(arg);
}
int main(int argc, char* argv[]) {
pthread_t th[11];
int i;
for (i = 0; i < 11; i++) {
int* a = malloc(sizeof(int));
*a = i;
if (pthread_create(&th[i], NULL, &routine, a) != 0) {
perror("Failed to created thread");
}
}
for (i = 0; i < 11; i++) {
if (pthread_join(th[i], NULL) != 0) {
perror("Failed to join thread");
}
}
return 0;
}
\ No newline at end of file
Loading