Linux下线程的挂起和恢复 http://blog.chinaunix.net/uid-21765995-id-1815655.html
来源:互联网 发布:windows自带图片浏览器 编辑:程序博客网 时间:2024/06/05 15:21
Linux下线程的挂起和恢复 2009-08-27 16:29:01
分类: LINUX
POSIX的Linux操作系统没有提供线程挂起和恢复的例程,在网上找了找,看到一个老外写的程序,感觉想法不错,放在这里大家分享一下。理论上应该可以实现,不过我没有试,给大家提供一个参考。 (在读取缓存里的数据时,当缓存中没有数据最好把线程挂起)
void CPrcThread ::suspend()
{
ifdef WIN32
//do windows specific things here...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
flag--;
pthread_mutex_unlock(&mutex);
#endif
}
void CPrcThread ::resume()
{
#ifdef WIN32
//do windows specific things here...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
flag++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
#endif
}
void* CPrcThread ::threadFunc(void* pParameter)
{
while(1)
{
#ifdef WIN32
//do windows specific things here...
//no member variables accessed here so its ok...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
while(flag <= 0)
{
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
#endif
//actual thread work here
}
}
void CPrcThread ::suspend()
{
ifdef WIN32
//do windows specific things here...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
flag--;
pthread_mutex_unlock(&mutex);
#endif
}
void CPrcThread ::resume()
{
#ifdef WIN32
//do windows specific things here...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
flag++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
#endif
}
void* CPrcThread ::threadFunc(void* pParameter)
{
while(1)
{
#ifdef WIN32
//do windows specific things here...
//no member variables accessed here so its ok...
#endif
#ifdef __linux__
pthread_mutex_lock(&mutex);
while(flag <= 0)
{
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
#endif
//actual thread work here
}
}
0 0
- Linux下线程的挂起和恢复 http://blog.chinaunix.net/uid-21765995-id-1815655.html
- Linux线程之线程栈 http://blog.chinaunix.net/uid-22590270-id-3673013.html
- linux系统/var/log目录下的信息详解 http://blog.chinaunix.net/uid-24250828-id-3198922.html
- Linux内核裁剪的具体步骤 http://blog.chinaunix.net/uid-10429687-id-3977273.html
- 配额的软限制和硬限制 http://blog.chinaunix.net/uid-23177306-id-2531124.html
- linux下pthread_mutex_lock,pthread_mutex_unlock的使用http://blog.chinaunix.net/uid-28458801-id-4300492.ht
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- http://blog.chinaunix.net/uid-20322254-id-145835.html
- http://blog.chinaunix.net/uid-25547034-id-3155778.html
- http://blog.chinaunix.net/uid-25750954-id-2956084.html
- http://blog.chinaunix.net/uid-22342564-id-3053393.html
- http://blog.chinaunix.net/uid-25082381-id-3242162.html
- http://blog.chinaunix.net/uid-25737580-id-3182286.html
- http://blog.chinaunix.net/uid-25835268-id-3055356.html--makefile
- http://blog.chinaunix.net/uid-8874157-id-2012678.html
- http://blog.chinaunix.net/uid-21289517-id-3081793.html
- http://blog.chinaunix.net/uid-20543672-id-3244832.html
- http://blog.chinaunix.net/uid-21222282-id-3244532.html
- Android图片优化问题总结
- 安装Red Hat Enterprise Linux 6.6
- 【Html】table标签,认识网页上的表格
- 启动MongoDB的授权认证校验
- MySQL编码设置方法 MySQL编码为utf8设置方法
- Linux下线程的挂起和恢复 http://blog.chinaunix.net/uid-21765995-id-1815655.html
- The application could not be verified
- Oracle 基本操作备忘[2]
- [数理统计-逐步回归]国内旅游收入回归
- C语言中的链表
- c++测试
- 十项颠覆性的存储技术
- 【OpenCV】SIFT特征检测器进行图像匹配
- LibGDX 跨平台游戏开发基础教程(总目录)