设为首页 加入收藏

TOP

Linux下线程的挂起和恢复
2014-11-24 00:45:19 来源: 作者: 【 】 浏览:4
Tags: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


}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux内核编译(2.6.18内核升级到V.. 下一篇Linux对一个3G的文本进行编码转换..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: