Linux多线程编程 - sleep 和 pthread_cond_timedwait
来源:互联网 发布:政府服务平台软件 编辑:程序博客网 时间:2024/05/21 09:53
#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进行【解锁和执行对条件的等待】(原子操作)。使用pthread_cond_timedwait()函数,一旦收到信号,就会立即唤醒。比sleep效率高很多。。。
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- linux多线程编程,你还在用sleep么?用pthread_cond_timedwait吧
- pthread_cond_timedwait 代替sleep
- 利用pthread_cond_timedwait实现sleep
- 多线程编程中sleep和WaitForSingleObject
- linux多线程编程,替代sleep的几种方式
- linux多线程编程,替代sleep的几种方式
- linux pthread_cond_timedwait() 线程同步
- pthread_cond_wait和pthread_cond_timedwait学习
- 多线程的sleep和wait
- 【多线程-Sleep()和Interrup()方法】
- 多线程的wait()和sleep()
- pthread_cond_timedwait
- pthread_cond_timedwait()
- "_OBJC_CLASS_$_CMMotionManager", referenced from:
- int和integer
- the science of programming BNF范式的优先级
- 使用gdb调试postfix
- Chrome浏览器安装开发者模式的扩展程序方法
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- 进制转换与原码反码补码
- makefile文件名函数使用
- CodeForces 487C Prefix Product Sequence
- iOS:navigationItem自定义时无法紧贴左侧
- Demo讲解之Lua和C++牵手
- performSelector may cause a leak because its selector is unknown
- HDFS配置项
- eclipse java代码和xml智能提示