Linux多线程编程,替代sleep的几种方式

Linux多线程编程,替代sleep的几种方式,第1张

我只想要进程的某个线程休眠一段时间的,可是用sleep()是将整个进程都休眠的,这个可能就达不到,我们想要的效果了。 目前我知道有三种方式:

1 usleep

这个是轻量级的, 听说能可一实现线程休眠, 我个人并不喜欢这种方式,所以我没有验证它的可行信(个人不推荐)。

2 select

这个可以,我也用过这种方式, 它是在轮询。

3 pthread_cond_timedwait

采用pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t *mutex, const struct timespec *abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演示一个demo。

首先,看这段代码,thr_fn为一个线程函数

#include <stdio.h>#include <stdlib.h>int flag = 1void * thr_fn(void * arg) {while (flag){printf("******\n")sleep(10)}printf("sleep test thread exit\n")}int main() {pthread_t threadif (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 = 0printf("Wait for thread to exit\n")pthread_join(thread, NULL)printf("Bye\n")return 0}

输入q后,需要等线程从sleep中醒来(由挂起状态变为运行状态),即最坏情况要等10s,线程才会被join。采用sleep的缺点:不能及时唤醒线程。

采用pthread_cond_timedwait函数实现的如下:

#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <pthread.h>#include <errno.h>static pthread_t threadstatic pthread_cond_t condstatic pthread_mutex_t mutexstatic int flag = 1void * thr_fn(void * arg) {struct timeval nowstruct timespec outtimepthread_mutex_lock(&mutex)while (flag) {printf("*****\n")gettimeofday(&now, NULL)outtime.tv_sec = now.tv_sec + 5outtime.tv_nsec = now.tv_usec * 1000pthread_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 = 0pthread_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进行【解锁和执行对条件的等待】(原子 *** 作)。这里的原子意味着:解锁和执行条件的等待是原则的,一体的。(In this case, atomically means with respect to the mutex andthe condition variable and other access by threads to those objectsthrough the pthread condition variable interfaces.)

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

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

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

使用权限 : 所有使用者 使用方式 : sleep [--help] [--version] number[smhd] 说明 : sleep 可以用来将目前动作延迟一段时间 参数说明 : --help : 显示辅助讯息 --version : 显示版本编号 number : 时间长度,后面可接 s、m、h 或 d 其中 s 为秒,m 为 分钟,h 为小时,d 为日数 例子 : 显示目前时间后延迟 1 分钟,之后再次显示时间 : datesleep 1mdate 这个命令更多应用于shell脚本编程里和程序里 如下面的一段程序: 应用程序: 复制代码代码如下:#include <syswait.h>usleep(n) //n微秒 Sleep(n)//n毫秒 sleep(n)//n秒 驱动程序: #include <linux/delay.h>mdelay(n) //milliseconds 其实现 #ifdef notdef #define mdelay(n) (\ {unsigned long msec=(n)while (msec--) udelay(1000)}) #else #define mdelay(n) (\ (__builtin_constant_p(n) &&(n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : \ ({unsigned long msec=(n)while (msec--) udelay(1000)})) #endif 调用asm/delay.h的udelay,udelay应该是纳秒级的延时 Dos: sleep(1)//停留1秒 delay(100)//停留100毫秒 Windows: Sleep(100)//停留100毫秒 Linux: sleep(1)//停留1秒 usleep(1000)//停留1毫秒 每一个平台不太一样,最好自己定义一套跨平台的宏进行控制 秒还是微秒?关于延时函数sleep() 因为要写一段代码,需要用到sleep()函数,在我印象中,sleep(10)好像是休眠10微秒,结果却是休眠了10秒(在Linux下)。觉得很奇怪,因为头儿也记得好像是微秒为单位的。所以就查了一下。 原来linux下的sleep函数原型为: unsigned int sleep(unsigned int seconds)而MFC中的Sleep函数原型为: void Sleep(DWORD dwMilliseconds)也就是说,Linux下(使用的gcc的库),sleep()函数是以秒为单位的,sleep(1)就是休眠1秒。而MFC下的sleep()函数是以微秒为单位的,sleep(1000)才是休眠1秒。原来如此啊。而如果在Linux下也用微妙为单位休眠,可以使用线程休眠函数:void usleep(unsigned long usec)当然,使用的时候别忘记#include <system.h>哦。 另外值得一提的是,linux下还有个delay()函数,原型为extern void delay(unsigned int msec)它可以延时msec*4毫秒,也就是如果想延时一秒钟的话,可以这么用 delay(250)

程序代码test.c共两个线程,一个主线程,一个读缓存区的线程:

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

char globe_buffer[100]

void *read_buffer_thread(void *arg) //这里先声明一下读缓存的线程,具体实现写在后面了

int main()

{

int res,i

pthread_t read_thread

for(i=0i<20i++)

globe_buffer[i]=i

printf("\nTest thread : write buffer finish\n")

sleep(3)\\这里的3秒是多余,可以不要。

res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL)

if (res != 0)

{

printf("Read Thread creat Error!")

exit(0)

}

sleep(1)

printf("waiting for read thread to finish...\n")

res = pthread_join(read_thread, NULL)

if (res != 0)

{

printf("read thread join failed!\n")

exit(0)

}

printf("read thread test OK, have fun!! exit ByeBye\n")

return 0

}

void *read_buffer_thread(void *arg)

{

int i,x

printf("Read buffer thread read data : \n")

for(i=0i<20i++)

{

x=globe_buffer[i]

printf("%d ",x)

globe_buffer[i]=0//清空

}

printf("\nread over\n")

}

---------------------------------------------------------------------------------

以上程序编译:

gcc -D_REENTRANT test.c -o test.o –lpthread

运行这个程序:

$ ./test.o:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8743236.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-20
下一篇2023-04-20

发表评论

登录后才能评论

评论列表(0条)

    保存