nanosleep()函数使用
2016-07-08 16:24
239 查看
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">nanosleep()函数会导致当前的线程将暂停执行,直到rqtp参数所指定的时间间隔。或者在指定时间间隔内有信号传递到当前线程,将引起当前线程调用信号捕获函数或终止该线程。</span>
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">//线程<span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);">休眠1500ms</span></span>
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h> void* pthread_do(void *arg) { struct timespec ts, ts1; int count = 0; ts.tv_nsec = 500000000; // 1500ms ts.tv_sec = 1; while( 1 ) { printf("sub pthread %d\n", count); count++; if ( nanosleep(&ts, &ts1) == -1 ) { printf("error!\n"); exit(1); } } return NULL; } int main(void) { pthread_t pthd; int count = 0; pthd = pthread_create(&pthd, NULL, pthread_do, NULL); while ( 1 ) { printf("main pthread %d\n", count); count++; sleep(1); } return 0; }
[gfj@kfjk2 c]$ ./a.out
main pthread 0
sub pthread 0
main pthread 1
sub pthread 1
main pthread 2
sub pthread 2
main pthread 3
main pthread 4
sub pthread 3
main pthread 5
sub pthread 4
main pthread 6
main pthread 7
sub pthread 5
main pthread 8
sub pthread 6
main pthread 9
main pthread 10
sub pthread 7
main pthread 11
sub pthread 8
main pthread 12
main pthread 13
sub pthread 9
相关文章推荐
- 如何快速上手使用OpenGL进行图形绘制
- text-align: justify
- JAVA POI关闭Workbook
- 剑指offer面试题5:从尾到头打印链表
- oracle粗略使用
- shell 检查是否是root用户
- css中visibility与display的区别
- spring+shiro 整合之自己注册会话和自写realm
- 修改SEAndroid的模式
- 【Servlet】脱离IDE的Servlet实现
- mysql之视图
- 无锁化编程有哪些常见方法?----阿里巴巴2015实习生笔试题
- 零起点学 C++ 第二章
- Angular-Ui-Router+ocLazyLoad动态加载脚本
- poj2823 Sliding Window
- Hive常用的SQL命令操作
- 时间空间
- 手势的3个混淆属性
- 横竖屏切换
- python 文件下载 方法