#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. Luogu P4168 [Violet]蒲公英 分块

    这道题算是好好写了.写了三种方法. 有一个好像是$qwq$$N\sqrt(N)$的方法,,但是恳请大佬们帮我看看为什么这么慢$qwq$(后面的第三种) 注:$pos[i]$表示$i$属于第$pos[i ...

  2. css(name|pro|[,val|fn])

    css(name|pro|[,val|fn]) 概述 访问匹配元素的样式属性.大理石平台支架 jQuery 1.8中,当你使用CSS属性在css()或animate()中,我们将根据浏览器自动加上前缀 ...

  3. 015_linuxC++之_覆写

    34.类成员函数的重载.覆盖和隐藏区别?答案:a.成员函数被重载的特征:(1)相同的范围(在同一个类中):(2)函数名字相同:(3)参数不同:(4)virtual 关键字可有可无.b.覆盖是指派生类函 ...

  4. vue项目更换目录后执行npm run dev 就报错(新手进)

    在我们搭建好一个VUE项目的环境后,觉得这个项目存放的位置不好,想移动一下,但是移动后我们发现执行npm run dev就会报下面的错误: 明明只是移动了一下位置,就报错,实在是太恶心了. 但是只要我 ...

  5. 【java设计模式】-00目录

    开篇 [java设计模式]-01设计模式简介 创建型模式: [java设计模式]-02工厂模式(Factory Pattern) [java设计模式]-03抽象工厂模式(Abstract Factor ...

  6. 2017 ZSTU寒假排位赛 #7

    题目链接:https://vjudge.net/contest/149498#overview. A题,水题,直接按照题意模拟一下即可. B题,我用的是线段树.大力用的差分标记(上次听zy说过,下次再 ...

  7. buff/cache占用过高的问题

    工作记录 > /proc/sys/vm/drop_caches 默认是0,不清除缓冲区缓存和页面缓存 可用值 0 到 3 值越高系统上的程序会跑起来越慢 free -m 蛋疼的是这只是一次性的, ...

  8. polya定理,环形涂色

    环形涂色裸题 #include<iostream> #include<cstdio> #include<algorithm> #include<vector& ...

  9. How to use reminder feature of the outlook

    https://support.office.com/en-us/article/set-or-remove-reminders-7a992377-ca93-4ddd-a711-851ef359792 ...

  10. C++ STL——类型转换

    目录 一 类型转换 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 类型转换 类型转换的含义是通过改变一个变量的类型为别的类型从而改变变量的表示方式 ...