forked from theArcticOcean/CLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadFinishBySem.c
60 lines (58 loc) · 1009 Bytes
/
threadFinishBySem.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
sem_t *sem[10];
void *run(void *param){
int *tmp = (int *)malloc(sizeof(int));
*tmp = *((int *)param);
sem_wait(sem[*tmp]);
pthread_exit(tmp);
return NULL;
}
int main(){
pthread_t tid[10];
int tag[10] = {0,1,2,3,4,5,6,7,8,9};
int i;
for(i=0;i<10;i++){
char str[3] = {0};
str[0] = i+'0';
sem[i] = sem_open(str, O_CREAT, 0777);
}
for(i=0;i<10;i++){
pthread_create(&tid[i],NULL,run, &tag[i]);
printf("create tid %d.\n",i);
}
for(i=0;i<10;i++){
sem_post(sem[i]);
}
for(i=0;i<10;i++){
int *ret;
pthread_join(tid[i],&ret);
sem_close(sem[i]);
printf("tid %d finished.\n",*ret);
free(ret);
}
return 0;
}
/*
create tid 0.
create tid 1.
create tid 2.
create tid 3.
create tid 4.
create tid 5.
create tid 6.
create tid 7.
create tid 8.
create tid 9.
tid 0 finished.
tid 1 finished.
tid 2 finished.
tid 3 finished.
tid 4 finished.
tid 5 finished.
tid 6 finished.
tid 7 finished.
tid 8 finished.
tid 9 finished.
*/