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);
}