linux线程挂起编程,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

}

}0b1331709591d260c1c78e86d0c51c18.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值