linux 多线程 多定时器

linux则只允许单进程拥有一个定时器,因此在linux下的单进程中要使用多个定时器,则需要自己维护管理。

方法一:

进程中只有一个定时器,通过标志位,可以在不同时间执行不同的任务。

方法二:

Linux进程多个定时器  实现数据更新 数据上传

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sqlite3.h"
#include "uhdt.h"
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include<iostream>
#include <errno.h>
#include <time.h>
#include <sys/sysinfo.h>
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include "read_line_conf.h"
#include "sqlite.h"
#include <signal.h>


#define TIME_WHEEL_SIZE 8
#define SN_CONF "./sn.conf"
#define _DEBUG_
#define TIME_WHEEL_SIZE 8
#define NetWorkCard "eth0"

typedef void (*func)(int data);

struct timer_node {
    struct timer_node *next;
    int rotation;
    func proc;
    int data;
};

struct timer_wheel {
    struct timer_node *slot[TIME_WHEEL_SIZE];
    int current;
};

struct timer_wheel timer = {{0}, 0};

void tick(int signo)
{
    // 使用二级指针删进行单链表的删除
    struct timer_node **cur = &timer.slot[timer.current];
    while (*cur) {
        struct timer_node *curr = *cur;
        if (curr->rotation > 0) {
            curr->rotation--;
            cur = &curr->next;
        } else {
            curr->proc(curr->data);
            *cur = curr->next;
            free(curr);
        }
    }
    timer.current = (timer.current + 1) % TIME_WHEEL_SIZE;
    alarm(1);
}

void add_timer(int len, func action)
{
    int pos = (len + timer.current) % TIME_WHEEL_SIZE;
    struct timer_node *node = (struct timer_node *)malloc(sizeof(struct timer_node));

    // 插入到对应格子的链表头部即可, O(1)复杂度
    node->next = timer.slot[pos];
    timer.slot[pos] = node;
    node->rotation = len / TIME_WHEEL_SIZE;
    node->data = 0;
    node->proc = action;
}

int InformationCount=0;
int InformationUploadCount=0;
void do_Information(int data)

if(InformationCount%1==0&&InformationCount>0){     
#ifdef _DEBUG_
    printf("Information Information\n");
#endif
}
InformationCount++;
     add_timer(5, do_Information);

}

// test case3: 5s循环定时器
void do_InformationUpload(int data)
{
if(InformationUploadCount%5==0&&InformationUploadCount>0){
#ifdef _DEBUG_
    printf("InformationUpload InformationUpload\n");
#endif
}
InformationUploadCount++;
add_timer(3, do_InformationUpload);

}

// 线程Information 方法
void * Information(void *a){
printf("void * Information(void *a)\n");
    //time begin
   add_timer(0, do_Information);
    // time end
    while(1);
    return NULL;
}

// 线程InformationUpload 方法
void * InformationUpload(void *b){
printf("void * InformationUpload(void *b)\n");
    //time begin
    add_timer(0, do_InformationUpload);
    // time end
    while(1);
    return NULL;
    
}
/*********timer end*********/

void threadInit(){

    
    
    pthread_t t0;
    pthread_t t1;

    // 创建线程Information
    if(pthread_create(&t0, NULL, Information, NULL) == -1){
        puts("fail to create pthread t0");
        exit(1);
    }
    //InformationUpload
    if(pthread_create(&t1, NULL, InformationUpload, NULL) == -1){
        puts("fail to create pthread t1");
        exit(1);
    }
 
    // 等待线程结束
    void * result;
    if(pthread_join(t0, &result) == -1){
        puts("fail to recollect t0");
        exit(1);
    }

    if(pthread_join(t1, &result) == -1){
        puts("fail to recollect t1");
        exit(1);
    }
#ifdef _DEBUG_
    printf("t0--------:%d\n",pthread_join(t0, &result));
    printf("t1---------%d\n",pthread_join(t1, &result));
#endif
}


int main( void )
{  
    signal(SIGALRM, tick);
    alarm(1); // 1s的周期心跳
    threadInit();//thread init
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42627035/article/details/84887791