采用pthread_cond_timedwait函数实现的如下:

#include<stdio.h>
#include <sys/time.h>
#include<unistd.h>
#include <pthread.h>
#include<errno.h>

static pthread_t thread;
staticpthread_cond_t cond;
static pthread_mutex_t mutex;
staticint 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");
}

intmain(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 thethread!\n");

pthread_mutex_lock(&mutex);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
printf("Wait forthread 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进行【解锁和执行对条件的等待】(原子操作)。这里的原子意味着:解锁和执行条件的等待是原则的,一体的。(Inthis caseatomicallymeans with respect to the mutex andthe condition variable and otheraccess by threads to those objectsthrough the pthread conditionvariable interfaces.

  如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。

  等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。

  尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

sleep是让线程去睡觉,它释放资源,让CPU切换到别的线程的执行,会消耗切换时间。

感觉还是wait*系列函数更好一些
因为可以在完成相同功能的情况占用更少的CPU资源,实时性要好一些。
比如串口通讯的握手过程,使用Wait*系列函数,可以发送完毕后设置超值为30秒,当是当有数据到达时以立即停止等待,处理数据。
如果使用Sleep只能有两种方法,循环执行多个短时间的Sleep,或者Sleep(30000)。如果前一种情况因为在循环,所以会占用CPU资源。如果有后一种方法,当数据在30内到达时将会反应迟钝。

linux中利用pthread库做多线程编程,如果使用简单的sleep系统调用,如果对pthread的实现不是很清楚的话,或者linux内核的sleep系统调用和pthread库配合的不是很好的话,在线程里面进行sleep系统调用的话,可能导致所有的线程休眠。或者在main函数里面调用sleep,在特定版本的linux内核中可能导致程序所有线程都休眠。这可能是不希望的,所以可以用异步IO实现“安全”的线程休眠。具体用select实现即可:

#include<sys/time.h>

#include<sys/types.h>

#include<unistd.h>

#include<stdio.h>

intmySleep(unsigned int sleepSecond)
{
structtimeval t_timeval;
t_timeval.tv_sec =sleepSecond;
t_timeval.tv_usec = 0;
select( 0, NULL, NULL, NULL, &t_timeval );
return 0;
}


这就可以绕过可能的“陷阱”。

在嵌入式应用里面,除去通用的库,出于空间的考虑,需要把一些程序进行静态链接,如果在实现sleep这个程序的时候,使用这个mySleep调用。进行静态链接的时候会产生出很大的可执行程序。

例如:


intmain(int argc,char ** argv)
{
if(argc==2)
{
int secNumber=0;
secNumber=atoi(argv[1]);
mySleep(secNumber);
}
else
{


printf("Usage:%s <second number>/n",argv[0]);
}
return 0;
}


进行:g++-o sleep --static sleep.cpp
得到的sleep可执行程序超过了1M

而使用传统的sleep系统调用编制sleep程序,在控制程序大小方面更好:

intmain(int argc,char ** argv)
{
if(argc ==2)
{
sleep(atoi(argv[1]));
}
else
{
printf("Usage:%s <sec Number>/n",argv[0]);
}
return 0;
}


进行:gcc-o sleep --static sleep.c
得到的sleep可执行程序仅仅400K左右。

很好。


Linux下调用sleep是用时钟的,一个进程的时钟系统是有限制的.如果每个线程使用sleep,到了最大的数量,最终会进程会挂起.最好是用select取代

voidselect_sleep(int i)
{
struct timeval timeout;

timeout.tv_sec= i;
timeout.tv_usec = 0;

}







更多相关文章

  1. Linux下cfsetospeed和cfsetispeed函数
  2. 字符串处理函数strcat和strtok
  3. Linux的时间函数(转载)
  4. linux进程和线程排查 · 记一次JVM CPU高负载的排查办法
  5. linux下main thread如何使用pthread_join等待子线程结束后再退出
  6. 【linux】下的mkfifo 命令 和【C语言】中的mkfifo函数
  7. linux c (4) 进程终止-exit和_exit函数
  8. 看谁能找出bug★☆open函数总是返回-1
  9. 【Ubuntu手记】开发多线程程序时在eclipse中添加libpthread.a库

随机推荐

  1. 【Android 进阶】ButterKnife-黄油刀
  2. 对于Android的文件结构介绍
  3. Android 国际化 语言适配
  4. Android 科大讯飞 在线和离线语音听写
  5. android声音播放之SoundPool的应用,让你的
  6. android listview多视图嵌套多视图
  7. Android高可拓展框架Conductor源码解析(2
  8. Android动画精讲一:从setTranslationX谈属
  9. 安卓中MVC模式的深度思索和实践(二)
  10. Android 通知(Notification)的基本用法