Linux下线程的挂起和恢复 http://blog.chinaunix.net/uid-21765995-id-1815655.html
2015-12-05 14:12
627 查看
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
}
}
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
}
}
相关文章推荐
- 浅析TCP字节流与UDP数据报的区别
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Android中Socket通信之TCP与UDP传输原理
- adb的一些使用
- cisco 交换机设置时区、时间、同步日志本地时间等操作
- Cisco交换机密码初始化
- groovy servlet java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
- http请求和数据安全
- iOS美丽约网络请求分析
- 深入了解HTTP协议
- 第五章IP基本原理
- android webview无网络情况下的处理
- 郭文军讲解“微博”如何与【微商城】进行绑定?
- android(20)(从网络抓取html显示在本地,把流转换成String,请求网络,简单的消息处理机制,编码问题)
- Linux网络服务之DNS
- 对李博士随机网络网络演算学位论文的学习
- 项目忽然出现 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
- android(19)(两种方式从网络抓取图片显示在本地)
- 从epoll构建muduo-11 单线程Reactor网络模型成型
- VC多线程编程(转载)http://www.cnblogs.com/xmsnzs/articles/1267436.html