Linux多线程编程 - sleep 和 pthread_cond_timedwait
#include <stdio.h> #include <stdlib.h>
int flag = 1; void * thr_fn(void * arg) { while (flag){ printf("******\n"); sleep(10); } printf("sleep test thread exit\n"); } 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函数,条件到了,线程即会被join,可及时唤醒线程。实现的如下:
#include <stdio.h> #include <sys/time.h> #include <unistd.h> #include <pthread.h> #include <errno.h> 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进行【解锁和执行对条件的等待】(原子操作)。
Linux多线程编程 - sleep 和 pthread_cond_timedwait的更多相关文章
- Linux多线程编程小结
Linux多线程编程小结 前一段时间由于开题的事情一直耽搁了我搞Linux的进度,搞的我之前学的东西都遗忘了,非常烦躁的说,如今抽个时间把之前所学的做个小节.文章内容主要总结于<Linux程序 ...
- Linux多线程编程初探
Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...
- ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程
为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...
- Linux多线程编程之详细分析
线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我见到这样一道面试题: 是否熟悉POSIX多线程 ...
- Linux多线程编程阅读链接
1. 进程与线程的一个简单解释(阮一峰) 2. linux 多线程编程 3. Linux 的多线程编程的高效开发经验 (IBM)
- Linux多线程编程和Linux 2.6下的NPTL
Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...
- 【操作系统作业-lab4】 linux 多线程编程和调度器
linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux多线程编程(不限Linux)【转】
——本文一个例子展开,介绍Linux下面线程的操作.多线程的同步和互斥. 前言 线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步 ...
随机推荐
- 关于pe结构
每一种操作系统它最重要的格式就是它的可执行文件格式, 因为操作系统就是为了支持这些文件而生成的,内核里面有很多机制,也是配合这种文件格式设计的. 换句话说,这种文件格式也是适合操作系统设计的. 比如: ...
- (九)文档和视图,Invalidate,数据库编程
一.文档视图结构 文档类(CDocument):存储加载(读写)数据视图类(CView):显示和修改数据 1)单文档 a)文档模板:把框架窗口.文档.视图关联在一起b)文档类(CDocument): ...
- 新西达电调初始化代码,使用nodejs ffi技术调用wiringpi,代码使用typescript编写
这是我设计的F450四轴飞行器飞控代码的一部分 运行在orangepi-zero上,操作系统是armbian,思路是使用node-ffi调用wiringpi的so库与GPIO通信,然后控制端逻辑代码使 ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- mybatis 操作其他数据库的数据表
配置文件里面配置的数据库只是默认数据库,并不是只能操作默认数据库.(被自己蠢死了,唉) 1. 注解方式 使用BaseMapper方式操作数据表时,在表对应的实体类上的 @table 注解描述表名时加上 ...
- ARP 之 发送请求arp_solicit
概述 arp_solicit用来发送ARP请求,首先会根据ARP_ANNOUNCE参数来选取源地址,然后判断是否达到内核发送次数上限,未达到则调用内核arp_send_dst函数发送,如果达到上限,则 ...
- HTML属性操作
属性名 属性值 相关操作:读与取 一.属性读操作:元素.属性,其实在就是找到等号右边的值 代码为: <!DOCTYPE html> <html lang="en" ...
- ios-Runtime调用私有方法
有时在代码中会有需要调用私有方法的场景,如不想import太多头文件:想组件设计一些解耦的模块:查看别人模块中未暴露的代码进行分析等. 在 ios 中调用私有方法有很多种方式,主要是通过Runtime ...
- consul ocelot
consul配置完成后 新建.netcoreapi项目, nuget安装ocelot 添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置 Serv ...
- WPF学习笔记 - 数据绑定(在代码中)
在程序代码里,有两种设置绑定的方法,一种是调用FrameworkElement或FrameContentElement对象的SetBinding实例方法. 例如: Public MainWindow( ...