linux中sleep替代函数,linux线程函数中代替sleep的方法

采用pthread_cond_timedwait函数实现的如下:

#include

#include

#include

#include

#include

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 case,atomicallymeans 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

#include

#include

#include

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  /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 /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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值