我使用posix_timer创建定时器,
代码如下:
C/C++ code
#include<stdio.h>
#include<time.h>
#include<signal.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<iostream>
using namespace std;
void fun1(sigval_t v)
{
printf("122334\n");
}
timer_t tid1;
typedef void(*pFun)(sigval_t);
int SetTimer(pFun pfun,timer_t &tid,int seconds,int id)
{
struct sigevent se;
struct itimerspec ts, ots;项目可行性研究报告
memset(&se,0,sizeof(sigevent));
memset(&ts,0,sizeof(itimerspec));
memset(&ts,0,sizeof(itimerspec));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = pfun;
se.sigev_value.sival_int = id;
timer_create(CLOCK_REALTIME,&se,&tid);
ts.it_value.tv_sec = 3;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = seconds;
ts.it_interval.tv_nsec = 0;
if(timer_settime(tid,TIMER_ABSTIME,&ts,&ots) < 0)
{
return -1;
}
return 0;图书管理系统论文
}
int main ()
{
pFun pfun = fun1;
SetTimer(pfun,tid1,13,1);
for(int i =0;i<5;i++ )
{
printf("11111\n");
sleep(1);
}
timer_delete(tid1);
return 1;
}
使用valgrind进行内存检测,出现内存泄露情况
如下:
[root@localhost Debug]# valgrind --tool=memcheck --leak-check=yes ./timeUse
==7258== Memcheck, a memory error detector
==7258== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==7258== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==7258== Command: ./timeUse
==7258==
11111
122334
11111
11111
11111
11111
==7258==
==7258== HEAP SUMMARY:
==7258== in use at exit: 144 bytes in 1 blocks
==7258== total heap usage: 4 allocs, 3 frees, 352 bytes allocated
==7258==
==7258== 144 bytes in 1 blocks are possibly lost in loss record 1 of 1
==7258== at 0x4005F0D: calloc (vg_replace_malloc.c:593)
==7258== by 0x389F29: _dl_allocate_tls (in /lib/ld-2.12.so)
==7258== by 0x56B34F: pthread_create@@GLIBC_2.1 (in /lib/libpthread-2.12.so)
==7258== by 0x59D131: __start_helper_thread (in /lib/librt-2.12.so)
==7258== by 0x5708DF: pthread_once (in /lib/libpthread-2.12.so)
==7258== by 0x804875B: SetTimer(void (*)(sigval), void*&, int, int) (main.cpp:38)
==7258== by 0x80487E3: main (main.cpp:55)
==7258==
==7258== LEAK SUMMARY: