#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 P5022 旅行 搜索+贪心

    好吧...一直咕..现在才过...被卡常卡到爆... 写的垃圾版本,$n^2$无脑删边..可以发现走出来的是棵树...更优秀的及数据加强版先咕着...一定写.qwq #include<cstdi ...

  2. Enable file editing in Visual Studio's debug mode

    Visual Studio 使用及调试必知必会 http://www.cnblogs.com/luminji/p/3287728.html How do I enable file editing i ...

  3. LA 7043 International Collegiate Routing Contest 路由表 字典树离散化+bitset 银牌题

    题目链接:给你n(n<=3e4)个路由地址(注意有子网掩码现象), 路由地址:128.0.0.0/1的形式 要求你输出一个路由集合,其是给定的路由集合的补集,且个数越少越好 #include & ...

  4. List对象遍历时null判断

    使用for循环遍历list处理list元素时,对null值判断: 1.list为null时空指针异常 2.list不为空,但是list.size()=0时,不执行for循环内代码块 3.list.si ...

  5. epoll反应堆

    /* * epoll基于非阻塞I/O事件驱动 */ #include <stdio.h> #include <sys/socket.h> #include <sys/ep ...

  6. Java基础__Java中自定义集合类

    Java基础__Java中集合类 传送门 自定义MyArrayList集合实现:增加数据.取数据.查看集合中数据个数方法 package com.Gary; public class MyArrayL ...

  7. Java 标准 IO 流编程一览笔录( 上 )

    Java标准I/O知识体系图: 1.I/O是什么? I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 2.流 流是一个连续的数据流,可以从流中读 ...

  8. Imagetragick 命令执行漏洞(CVE-2016–3714)

    Imagetragick介绍: ImageMagick是一款使用量很广的图片处理程序,很多厂商都调用了这个程序进行图片处理,包括图片的伸缩.切割.水印.格式转换等等.但近来有研究者发现,当用户传入一个 ...

  9. Flume-概述

    Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统.Flume 基于流式架构,灵活简单. Flume最主要的作用就是,实时读取服务器本地磁盘的数据, ...

  10. 中间件 | kafka简介、使用场景、设计原理、主要配置及集群搭建

    开源Java学习 公众号 一.入门 1.简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性 ...