在linux环境中,如何实现多线程中使用多个定时器,POSIX定时器可以吗,如何用?

在linux环境中,如何实现多线程中使用多个定时器,POSIX定时器可以吗,如何用?,第1张

个人解决了,以下是一个实现:

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#include <signal.h>

#include <string.h>

#include <pthread.h>

#include <time.h>

#if 1

pthread_attr_t attr

timer_t hard_timer, software_timer

struct sigevent hard_evp, software_evp

static void watchdog_hard_timeout(union sigval v)

{

time_t t

char p[32]

timer_t *q

struct itimerspec ts

int ret

time(&t)

strftime(p, sizeof(p), "%T", localtime(&t))

printf("watchdog hard timeout!\n")

printf("%s thread %d, val = %u, signal captured.\n", p, (unsigned int)pthread_self(), v.sival_int)

q = (timer_t *)(v.sival_ptr)

printf("hard timer_t:%d add:%p, q:%p!\n", (int)hard_timer, &hard_timer, q)

ts.it_interval.tv_sec = 0

ts.it_interval.tv_nsec = 0

ts.it_value.tv_sec = 6

ts.it_value.tv_nsec = 0

ret = timer_settime(*q, CLOCK_REALTIME, &ts, NULL)

if (ret != 0) {

printf("settime err(%d)!\n", ret)

}

}

static void watchdog_software_timeout(union sigval v)

{

time_t t

char p[32]

timer_t *q

struct itimerspec ts

int ret

time(&t)

strftime(p, sizeof(p), "%T", localtime(&t))

printf("watchdog software timeout!\n")

printf("%s thread %d, val = %u, signal captured.\n", p, (unsigned int)pthread_self(), v.sival_int)

q = (timer_t *)(v.sival_ptr)

printf("hard timer_t:%d add:%p, q:%p!\n", (int)hard_timer, &hard_timer, q)

ts.it_interval.tv_sec = 0

ts.it_interval.tv_nsec = 0

ts.it_value.tv_sec = 10

ts.it_value.tv_nsec = 0

ret = timer_settime(*q, CLOCK_REALTIME, &ts, NULL)

if (ret != 0) {

printf("settime err(%d)!\n", ret)

}

}

static void dcmi_sol_pthread_attr_destroy(pthread_attr_t *attr)

{

pthread_attr_destroy(attr)

}

static int dcmi_sol_pthread_attr_init(pthread_attr_t *attr)

{

int ret

if ((ret = pthread_attr_init(attr) != 0)) {

goto err

}

if ((ret = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)) != 0) {

dcmi_sol_pthread_attr_destroy(attr)

goto err

}

/* 设置线程的栈大小,失败则用系统默认值 */

pthread_attr_setstacksize(attr, 128 * 1024)

return 0

err:

printf("set ptread attr failed(ret:%d)!\n", ret)

return -1

}

int main(void)

{

struct itimerspec ts

int ret

ret = dcmi_sol_pthread_attr_init(&attr)

if (ret != 0) {

printf("init pthread attributes fail(%d)!\n", ret)

exit(-1)

}

memset(&hard_evp, 0, sizeof(struct sigevent))

hard_evp.sigev_value.sival_ptr = &hard_timer

hard_evp.sigev_notify = SIGEV_THREAD

hard_evp.sigev_notify_function = watchdog_hard_timeout

hard_evp.sigev_notify_attributes = NULL//&attr

memset(&software_evp, 0, sizeof(struct sigevent))

software_evp.sigev_value.sival_ptr = &software_timer

software_evp.sigev_notify = SIGEV_THREAD

software_evp.sigev_notify_function = watchdog_software_timeout

software_evp.sigev_notify_attributes = NULL//&attr

ret = timer_create(CLOCK_REALTIME, &hard_evp, &hard_timer)

if(ret != 0) {

perror("hard timer_create fail!")

exit(-1)

}

ret = timer_create(CLOCK_REALTIME, &software_evp, &software_timer)

if (ret != 0) {

timer_delete(hard_timer)

perror("software timer_create fail!")

exit(-1)

}

ts.it_interval.tv_sec = 0

ts.it_interval.tv_nsec = 0

ts.it_value.tv_sec = 6

ts.it_value.tv_nsec = 0

ret = timer_settime(hard_timer, CLOCK_REALTIME, &ts, NULL)

if(ret != 0) {

perror("hard timer_settime fail!")

timer_delete(hard_timer)

timer_delete(software_timer)

exit(-1)

}

ts.it_value.tv_sec = 10

ret = timer_settime(software_timer, CLOCK_REALTIME, &ts, NULL)

if(ret != 0) {

perror("hard timer_settime fail!")

timer_delete(hard_timer)

timer_delete(software_timer)

exit(-1)

}

while(1) {

printf("main ready sleep!\n")

sleep(15)

printf("main sleep finish!\n")

}

return 0

}

#endif

Linuxsleep(0)是Linux内核中的一个系统调用,它可以让线程在指定的时间内进入睡眠状态,从而实现线程切换。它可以用来实现定时器功能,也可以用来实现线程同步。它的原理是,当线程调用linuxsleep(0)时,它会把自己放入睡眠队列,并且把当前的CPU时间片设置为0,这样就可以实现线程切换。当睡眠时间到达时,线程会被唤醒,然后继续执行。因此,linuxsleep(0)可以用来实现线程切换,从而提高系统的性能。

linux下不是有现成的异步定时器么,使用setitimer。linux下定时就3种方法,不使用setitimer的话,就是用sleep让进程休眠或者使用gettimeofday获取时间并比较超时。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/6195531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存