-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
47 lines (39 loc) · 897 Bytes
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "token_bucket.h"
static token_bucket_t *tb;
void *proc(void *arg) {
while(1) {
if(token_bucket_try_take(tb, 1)) {
printf("hello: %lu\n", pthread_self());
} else {
sleep(0);
}
}
}
void *tick(void *arg) {
while(1) {
sleep(1);
printf("=============================\n");
}
}
int main(int argc, char *argv[]) {
pthread_t t1, t2, t3, t4;
if(argc < 2) {
printf("Usage: ./test qps\n");
return 0;
}
int qps = atoi(argv[1]);
printf("set qps=%d\n", qps);
tb = token_bucket_create(qps, 1);
pthread_create(&t1, NULL, &proc, NULL);
pthread_create(&t2, NULL, &proc, NULL);
pthread_create(&t3, NULL, &proc, NULL);
pthread_create(&t4, NULL, &tick, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
token_bucket_destroy(tb);
return 0;
}