C++线程 定时器简单例程整理

C++线程 定时器简单例程整理

参考博客
添加链接描述
//文件名:test.cpp

#include
#include
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
// 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取
int tid = ((int)threadid);
cout << "Hello Runoob! 线程 ID, " << tid << endl;
pthread_exit(NULL);
}

int main ()
{
pthread_t threads[NUM_THREADS];
int indexes[NUM_THREADS];// 用数组来保存i的值
int rc;
int i;
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : 创建线程, " << i << endl;
indexes[i] = i; //先保存i的值
// 传入的时候必须强制转换为void* 类型,即无类型指针
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)&(indexes[i]));
if (rc){
cout << “Error:无法创建线程,” << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}

现在编译并执行程序,将产生下列结果:

$ g++ test.cpp -lpthread -o test.o
$ ./test.o
main() : 创建线程, 0
main() : 创建线程, 1
Hello Runoob! 线程 ID, 0
main() : 创建线程, Hello Runoob! 线程 ID, 21

main() : 创建线程, 3
Hello Runoob! 线程 ID, 2
main() : 创建线程, 4
Hello Runoob! 线程 ID, 3
Hello Runoob! 线程 ID, 4

向线程传递参数
这个实例演示了如何通过结构传递多个参数。您可以在线程回调中传递任意的数据类型,因为它指向 void,如下面的实例所示:

#include
#include
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

struct thread_data{
int thread_id;
char *message;
};

void *PrintHello(void *threadarg)
{
struct thread_data *my_data;

my_data = (struct thread_data *) threadarg;

cout << "Thread ID : " << my_data->thread_id ;
cout << " Message : " << my_data->message << endl;

pthread_exit(NULL);
}

int main ()
{
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;

for( i=0; i < NUM_THREADS; i++ ){
cout <<"main() : creating thread, " << i << endl;
td[i].thread_id = i;
td[i].message = (char*)“This is message”;
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)&td[i]);
if (rc){
cout << “Error:unable to create thread,” << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}

定时器加epoll机制的例子
#include <sys/timerfd.h>
#include
#include <time.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <assert.h>
using namespace std;

const int MAXNUM = 20;

int main(int argc, char *argv[])
{
struct itimerspec new_value;
struct timespec now;
uint64_t exp;
ssize_t s;

int ret = clock_gettime(CLOCK_REALTIME, &now);//获取时钟时间
assert(ret != -1);


new_value.it_value.tv_sec = 5; //第一次到期的时间
new_value.it_value.tv_nsec = now.tv_nsec; 

new_value.it_interval.tv_sec = 1;      //之后每次到期的时间间隔
new_value.it_interval.tv_nsec = 0;

int timefd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK); // 构建了一个定时器
assert(timefd != -1);

ret = timerfd_settime(timefd, 0, &new_value, NULL);//启动定时器
assert(ret != -1);

cout << "timer started" << endl; // 定时器开启啦!


int epollfd = epoll_create(1);  //创建epoll实例对象

struct epoll_event ev;
struct epoll_event events[MAXNUM];
ev.data.fd = timefd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, timefd, &ev); //添加到epoll事件集合

for (; ;) 
{
    int num = epoll_wait(epollfd, events, MAXNUM, 0);
    assert(num >= 0);

    for (int i = 0; i < num; i++) 
    {
        if (events[i].events & EPOLLIN) 
        {
    //sleep(1);         //....处理其他事件
            if (events[i].data.fd == timefd) 
            {
                 s = read(timefd, &exp, sizeof(uint64_t)); //需要读出uint64_t大小, 不然会发生错误
                 cout<<exp<<endl;
                assert(s == sizeof(uint64_t));
                cout << "here is timer" << endl;
            }
        }
    }
    sleep(0。5);
}

close(timefd);


return 0;

}

如果有多个事件用单独线程监控 主线程做主线程的事情 可以用下面的例子

#include
#include <pthread.h>
#include <time.h>

#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <assert.h>
using namespace std;
#define NUM_THREADS 5
const int MAXNUM = 20;
// 线程的运行函数
void* say_hello(void* args)
{
struct itimerspec new_value;
struct timespec now;
uint64_t exp;
ssize_t s;

int ret = clock_gettime(CLOCK_REALTIME, &now);//获取时钟时间
assert(ret != -1);


new_value.it_value.tv_sec = 5; //第一次到期的时间
new_value.it_value.tv_nsec = now.tv_nsec; 

new_value.it_interval.tv_sec = 1;      //之后每次到期的时间间隔
new_value.it_interval.tv_nsec = 0;

int timefd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK); // 构建了一个定时器
assert(timefd != -1);

ret = timerfd_settime(timefd, 0, &new_value, NULL);//启动定时器
assert(ret != -1);

cout << "timer started" << endl; // 定时器开启啦!


int epollfd = epoll_create(1);  //创建epoll实例对象

struct epoll_event ev;
struct epoll_event events[MAXNUM];
ev.data.fd = timefd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, timefd, &ev); //添加到epoll事件集合

for (; ;) 
{
    int num = epoll_wait(epollfd, events, MAXNUM, 0);
    assert(num >= 0);

    for (int i = 0; i < num; i++) 
    {
        if (events[i].events & EPOLLIN) 
        {
    //sleep(1);         //....处理其他事件
            if (events[i].data.fd == timefd) 
            {
                 s = read(timefd, &exp, sizeof(uint64_t)); //需要读出uint64_t大小, 不然会发生错误
                 cout<<exp<<endl;
                assert(s == sizeof(uint64_t));
                cout << "here is timer" << endl;
            }
        }
    }
  
}

close(timefd);
return 0;

}

int main()
{

pthread_t tids[1];

//参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
int ret = pthread_create(&tids[0], NULL, say_hello, NULL);
if (ret != 0)
{
    cout << "pthread_create error: error_code=" << ret << endl;
}
while(1)
{
     cout<<"main****"<<endl;
     sleep(1);

}


//等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
pthread_exit(NULL);

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c6678是德州仪器(Texas Instruments)公司生产的一款高性能数字信号处理器(DSP),具有多个定时器模块可用于产生中断。多个定时器中断例程是指在c6678上同时或者交替使用多个定时器模块来实现中断功能。 在c6678上,每个定时器都有自己的控制寄存器和计数寄存器,可以通过设置定时器的控制寄存器来配置其工作模式和定时器周期。定时器中断例程主要包括以下几个步骤: 1. 初始化定时器:首先需要对定时器进行初始化,设置定时器的控制寄存器和计数寄存器的初始值,以及中断服务函数的地址。 2. 开启定时器中断:将定时器中断使能位设置为1,使得定时器计数到达设定的周期后触发中断。 3. 编写中断服务函数:当定时器产生中断时,CPU会跳转到中断服务函数,执行用户定义的中断处理程序。 4. 复位中断标志位:在中断服务函数中,需要手动清除定时器的中断标志位,以便定时器能再次产生中断。 5. 关闭定时器中断:如果需要关闭定时器中断,可以将定时器中断使能位设置为0,停止定时器的计数。 通过使用多个定时器模块,并根据需求设置不同的定时器周期,可以实现多个不同精度的定时器中断例程。这样可以更好地满足不同应用场景下的定时和计时需求。同时,c6678的多个定时器模块可以独立工作,互不干扰,提高了系统的并行性和响应速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值