我是靠谱客的博主 彪壮麦片,这篇文章主要介绍nanosleep()函数使用,现在分享给大家,希望可以做个参考。

复制代码
1
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">nanosleep()函数会导致当前的线程将暂停执行,直到rqtp参数所指定的时间间隔。或者在指定时间间隔内有信号传递到当前线程,将引起当前线程调用信号捕获函数或终止该线程。</span>
复制代码
1
<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>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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 %dn", 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 %dn", 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


最后

以上就是彪壮麦片最近收集整理的关于nanosleep()函数使用的全部内容,更多相关nanosleep()函数使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部