#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的更多相关文章

  1. Linux多线程编程小结

     Linux多线程编程小结 前一段时间由于开题的事情一直耽搁了我搞Linux的进度,搞的我之前学的东西都遗忘了,非常烦躁的说,如今抽个时间把之前所学的做个小节.文章内容主要总结于<Linux程序 ...

  2. Linux多线程编程初探

    Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...

  3. ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程

    为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...

  4. Linux多线程编程之详细分析

    线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我见到这样一道面试题: 是否熟悉POSIX多线程 ...

  5. Linux多线程编程阅读链接

    1. 进程与线程的一个简单解释(阮一峰) 2. linux 多线程编程 3. Linux 的多线程编程的高效开发经验 (IBM)

  6. Linux多线程编程和Linux 2.6下的NPTL

    Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...

  7. 【操作系统作业-lab4】 linux 多线程编程和调度器

    linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...

  8. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  9. Linux多线程编程(不限Linux)【转】

    ——本文一个例子展开,介绍Linux下面线程的操作.多线程的同步和互斥. 前言 线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步 ...

随机推荐

  1. Web自动化简介

    在迭代中时,先写出粗略的自动化测试用例,不是非常精准的一些提示,等到项目上线后,提示文案已经确定,再完善用例,使之成为一个完整的自动化测试工程. 用excel管理测试数据,读取效率比较低,需要读取.数 ...

  2. HTML JS 弹层后底部页面禁止滚动处理

    1.打开新页面时需要禁止鼠标滚轮,禁止页面滑动: 1 2 3 4 在调用显示层时加上这句js代码就可以了: document.documentElement.style.overflow = &quo ...

  3. luogu2634

    P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  4. C语言基础语法之指向函数的指针

    指针是C语言的精髓,对于初学者来讲,指针是C语言语法学习中比较难的知识点,而这里面指向函数的指针更是不太容易理解. 下面给大家讲下怎样学习理解C语言中指向函数的指针及编程方法和使用例子. 注意:这是一 ...

  5. Python字典元素的增加删除和取出字典所有的键和值

    一.增加一个或多个元素 d = {'a': 1} d.update(b=2) #也可以 d.update({‘b’: 2}) print(d) # {'a': 1, 'b': 2} d['e'] = ...

  6. Dubbo API 笔记——配置参考

    版权声明:欢迎转载,请注明出处,谢谢! https://blog.csdn.net/benhuo931115/article/details/78457391 schema 配置参考 所有配置项分为三 ...

  7. Linux设备驱动程序 之 RCU机制

    读取-复制-更新(read-copy-update,RCU)是一种高级的互斥机制,在正确的条件下,可以获得高的性能: RCU对它保护的数据结构做了一些限定,它针对经常发生读而很少发生写的情况做了优化, ...

  8. B站动手学深度学习第十八课:seq2seq(编码器和解码器)和注意力机制

    from mxnet import nd h_forward = nd.array([1,2]) h_backward = nd.array([3,4]) h_bi = nd.concat(h_for ...

  9. Python3.x运行Python2.x代码报错 syntax error "Missing parentheses in call to 'print'

    #另外一种错误 SyntaxError: Missing parentheses in call to 'print'. Did you mean print( 查看代码,格式如下: print &q ...

  10. sql拼接中的小错误

    字符串类型变量拼接到sql字符串上,容易忘记添加单引号,使用jdbcTemplate执行,报如下错误 正确写法如下: