linux多线程 进程休眠,转载:Linux多线程之线程休眠
我只想要进程的某个线程休眠一段时间的,可是用sleep()是将整个进程都休眠的,这个可能就达不到,我们想要的效果了。目前我知道有三种方式:
1、usleep
这个是轻量级的,听说能可一实现线程休眠,我个人并不喜欢这种方式,所以我没有验证它的可行信(个人不推荐)。
2、select
这个可以,我也用过这种方式,它是在轮询。
3、pthread_cond_timedwait
采用pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t
*mutex, const struct timespec
*abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演
示一个demo。
首先,看这段代码,thr_fn为一个线程函数:
#include
#include
int flag =
1;
void * thr_fn(void * arg) {
while (flag){
printf("******\n");
sleep(10);
}
printf("sleep
}
int main() {
pthread_t thread;
if (0 != pthread_create(&thread, NULL,
thr_fn, NULL)) {
printf("error when create pthread,%d\n", errno);
return
1;
}
char c ;
while ((c = getchar()) != 'q');
printf("Now terminate the thread!\n");
flag = 0;
printf("Wait for thread to exit\n");
pthread_join(thread, NULL);
printf("Bye\n");
return 0;
}
输入q后,需要等线程从sleep中醒来(由挂起状态变为运行状态),即最坏情况要等10s,线程才会被join。采用sleep的缺点:不能及时唤醒线程。
采用pthread_cond_timedwait函数实现的如下:
#include
#include
#include
#include
#include
static pthread_t thread;
static pthread_cond_t cond;
static pthread_mutex_t mutex;
static int flag = 1;
void * thr_fn(void * arg)
{
struct timeval now;
struct timespec outtime;
pthread_mutex_lock(&mutex);
while (flag) {
printf("*****\n");
gettimeofday(&now, NULL);
outtime.tv_sec = now.tv_sec + 5;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_cond_timedwait(&cond, &mutex,
&outtime);
}
pthread_mutex_unlock(&mutex);
printf("cond thread exit\n");
}
int main(void)
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (0 != pthread_create(&thread, NULL,
thr_fn, NULL)) {
printf("error when create pthread,%d\n", errno);
return
1;
}
char c ;
while ((c = getchar()) != 'q');
printf("Now terminate the
thread!\n");
pthread_mutex_lock(&mutex);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
printf("Wait for thread to exit\n");
pthread_join(thread, NULL);
printf("Bye\n");
return 0;
}
pthread_cond_timedwait()函数阻塞住调用该函数的线程,等待由cond指定的条件被触发(pthread_cond_broadcast()
or pthread_cond_signal())。
当pthread_cond_timedwait()被调用时,调用线程必须已经锁住了mutex。函数
pthread_cond_timedwait()会对mutex进行【解锁和执行对条件的等待】(原子操作)。这里的原子意味着:解锁和执行条件的等待
是原则的,一体的。(In this case, atomically means with respect to the mutex
andthe condition variable and other access by threads to those
objectsthrough the pthread condition variable
interfaces.)
如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。
等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。
尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。
linux多线程 进程休眠,转载:Linux多线程之线程休眠相关推荐
- linux 管道文件上机总结,[转载]LINUX 管道 fifo 等总结
Linux进程通信:命名管道FIFO小结 Linux下进程之间通信可以用命名管道FIFO完成.命名管道是一种特殊类型的文件,因为Linux中所有事物都是文件,它在文件系统中以文件名的形式存在. 在程序 ...
- linux端口进程号pid,Linux根据端口号查看进程PID
1.命令lsof,以查找占用端口80为例,用法如下: [root@localhost nginx]# lsof -i:80 以上为没有进程占用80端口, [root@localhost sbin]# ...
- linux php进程端口占用,linux如何查看端口占用情况
在linux中可以使用lsof和netstat命令来查看端口占用情况,其中lsof语法为"lsof -i:端口号",netstat语法为"netstat -tunlp | ...
- linux下进程监控6,Linux进程监控技术—精通软件性能测试与LoadRunner最佳实战(6)...
8.2.5 Linux操作系统进程监控技术 Linux在进程监控方面同样出色,不仅可以通过图形用户界面的管理工具,还可以用命令方式显示进程相关信息.像"Windows的任务管理器" ...
- linux下进程调度算法实验,Linux下进程调度算法的模拟实现.doc
Linux下进程调度算法的模拟实现 枣 庄 学 院 信息科学与工程学院课程设计任务书 题目: Linux下进程调度算法的模拟实现 学 生1: 学 生2: 学 生3: 专 业: 计算机应用技术 课 程: ...
- linux下jtag命令,[转载]LINUX内核调试过程(使用OpenJtag + OpenOCD)
[转载]LINUX内核调试过程(使用OpenJtag + OpenOCD) (2012-04-12 02:02:27) 标签: 杂谈 [转载]LINUX内核调试过程(使用OpenJtag + Open ...
- linux通过进程看端口,linux下通过进程名查看其占用端口
linux下通过进程名查看其占用端口: 1.先查看进程pid ps -ef | grep 进程名 2.通过pid查看占用端口 netstat -nap | grep 进程pid 例:通过nginx进程 ...
- linux 查进程对应用户,linux进程状态查询
系统维护的时候难免会遇到进程的状态的查询和管理,到底什么是R,有的是S,有的还是S+呢?一直有些混沌的问题,今天细细的来总结一下: ps是用来报告系统中程序执行状况的命令这个是无可厚非的,linux进 ...
- linux打开core文件,[转载]linux下core文件设置与查看
程序异常推出时,内核会生成一个core文件(是内存映像以及调试信息).可以通过使用gdb来查看core文件,指示出导致程序出错的代码所在的文件和行数. 1.查看系统中core文件生成的开关是否打开 1 ...
- linux runable进程查询,关于Linux下进程的详解【进程查看与管理】
一.关于进程 进程: 已经启动的可执行程序的运行实力 进程的组成:一个进程包含内核中的一部分地址空间和一系列数据结构.其中地址空间是内核标记的一部分内存以供进程使用,而数据结构则用来纪录每个进程的具体 ...
最新文章
- lingo calcinit
- android动态视力测试,Chop It-看起来有点无聊却可训练动态视力的 3D 立体小游戏(iPhone, Android)...
- YARN/MRv2 Resource Manager深入剖析—RM总体架构
- SpringBoot+Junit在IDEA中实现查询数据库的单元测试
- JSONUtils.toJSONString的一个坑
- Java操作shell脚本
- 微服务模块综合管理(模块视图管理,自动化热部署,前端资源实时刷新......)
- 比特币被盗事件频发,这次是 6300 万美元
- IT前景---网络工程师规划之路
- tcp序列号为什么是随机的_TCP与UDP
- 3.1、如何通过ISP(FlyMcu串口)下载程序(附CH340驱动及FlyMcu安装包)
- pclint vc2012配置
- SU插件|实时联动Lumion LiveSync for SketchUp免费下载(渲染器与草图大师模型同步更新)
- windows功能_windows必备的系统功能增强2具,免费,简单,实用
- 一元二次方程的解法(Python语言)
- linux gem安装软件,安装gem报错
- vue项目vscode常用插件
- 前端一(HTML、CSS)
- python表白玫瑰花绘制——情人节表白(亲测)
- 语雀全新模板中心,你的创作锦囊