红联Linux门户
Linux帮助

Linux下线程的挂起和恢复

发布时间:2008-09-21 00:37:38来源:红联作者:iuears
POSIX的Linux操作系统没有提供线程挂起和恢复的例程,在网上找了找,看到一个老外写的程序,感觉想法不错,放在这里大家分享一下。理论上应该可以实现,不过我没有试,给大家提供一个参考。[code]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

}

}[/code]
文章评论

共有 0 条评论