File size: 1,075 Bytes
bb16f10
 
83607bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bff97b
 
 
 
 
 
 
 
 
bb16f10
 
 
 
 
 
 
 
 
 
 
83607bc
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
#include "utils.h"
#include <sys/time.h>

int thread_num = 4;
hloop_t** worker_loops = NULL;

void init_loop(int _thread_num, hthread_routine worker_thread) {
    thread_num = _thread_num;

    worker_loops = (hloop_t**)malloc(sizeof(hloop_t*) * thread_num);
    for (int i = 0; i < thread_num; ++i) {
        worker_loops[i] = hloop_new(HLOOP_FLAG_AUTO_FREE);
        hthread_create(worker_thread, worker_loops[i]);
    }
}

hloop_t* get_next_loop() {
    static int s_cur_index = 0;
    if (s_cur_index == thread_num) {
        s_cur_index = 0;
    }
    return worker_loops[s_cur_index++];
}

char generateRandomKey()
{
    // 使用时间作为随机数种子,确保每次运行生成的随机数都不同
    srand((unsigned int)time(NULL));

    // 生成 'A' 到 'Z' 之间的随机字符
    return (char)('A' + rand() % 26);
}

long long currentTimeMillis() {
    struct timeval now;
    gettimeofday(&now, NULL);

//    return time(NULL);

//    struct timespec now;
//    clock_gettime(CLOCK_MONOTONIC, &now);
    return now.tv_sec * 1000 + now.tv_usec / 1000;
}