-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample02.c
More file actions
38 lines (33 loc) · 721 Bytes
/
Copy pathExample02.c
File metadata and controls
38 lines (33 loc) · 721 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
pthread_t tid[100];
struct timeval start, stop;
int count;
void* thread(void*);
int main(){
int i;
sem_init(&mutex,0,1);
for(i=0;i<100;++i){
pthread_create(&tid[i],NULL,thread,NULL);
}
for(i=0;i<100;++i)
pthread_join(tid[i],NULL);
sem_destroy(&mutex);
return 0;
}
void* thread(void* arg){
sem_wait(&mutex);
gettimeofday(&start,NULL);
sleep(1);
(void)printf("%d - ", count++);
gettimeofday(&stop,NULL);
printf("%d - ", (start.tv_sec));
sem_post(&mutex);
printf("%d\n", (stop.tv_sec));
return NULL;
}