手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下线程的挂起和恢复

[日期:2008-09-21] 来源:Linux社区  作者:Linux编辑 [字体: ]

POSIX的Linux操作系统没有提供线程挂起和恢复的例程,在网上找了找,看到一个老外写的程序,感觉想法不错,放在这里大家分享一下。理论上应该可以实现,不过我没有试,给大家提供一个参考。

void CPrcThread<Worker>::suspend()
{
ifdef WIN32
//do windows specific things here...
#endif

#ifdef __linux__
pthread_mutex_lock(&mutex);
flag--;
pthread_mutex_unlock(&mutex);
#endif
}

void CPrcThread<Worker>::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<Worker>::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
相关资讯       Linux教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款