-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleepsort.c
More file actions
41 lines (32 loc) · 773 Bytes
/
sleepsort.c
File metadata and controls
41 lines (32 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
volatile int running = 0;
pthread_mutex_t running_mutex;
void *ssort(void *ptr) {
unsigned int d = *((unsigned int *) ptr);
sleep(d);
printf("%d ", d);
pthread_mutex_lock(&running_mutex);
running--;
pthread_mutex_unlock(&running_mutex);
return NULL;
}
int main(int argc, char **argv) {
pthread_mutex_init(&running_mutex, NULL);
int data[10] = {5,1,9,6,2,8,7,3,4};
for (int i=0; i < 10; i++) {
// über mutex erhöhen
pthread_mutex_lock(&running_mutex);
running++;
pthread_mutex_unlock(&running_mutex);
// thread starten
pthread_t t;
pthread_create(&t, NULL, ssort, (void *) &data[i]);
}
while (running > 0) {
sleep(1);
}
pthread_mutex_destroy(&running_mutex);
return 0;
}