有关pthread线程的暂停与恢复的讨论
2014-03-26 14:15
295 查看
pthead线程的暂停与恢复的讨论,一个非常好的回答 http://bbs.csdn.net/topics/390321574
希望对自己和有需要的朋友有很大的帮助。。。。
自己参考那个,选择和修改了那位兄弟的部分代码 ,,现在可以实现那个功能
希望对自己和有需要的朋友有很大的帮助。。。。
自己参考那个,选择和修改了那位兄弟的部分代码 ,,现在可以实现那个功能
void pthread_suspend(void) { if (pthread_pause == false) { pthread_mutex_lock( &mutex_pause ); pthread_pause = true; printf("\n------transport pause------\n"); pthread_mutex_unlock( &mutex_pause ); } else { printf("the transport suspend already/n"); } } void pthread_resume(void) { if (pthread_pause == true) { pthread_mutex_lock(&mutex_pause); pthread_pause = false; pthread_cond_broadcast(&cond_pause); printf("------transport resume------/n"); pthread_mutex_unlock(&mutex_pause); } else { printf("transport resume already/n"); } } void* threadFunc(void* args) //工作线程函数 { while(1) { if(!pthread_pause) { printf("working..."); //work(); } else { printf("rest..."); } sleep(1); } return ((void*)0); } void* threadFunc(void* args) //工作线程函数 { while(1) { if(pthread_pause == false) { work(); } else { pthread_cond_wait(&cond_pause,&mutex_pause); } } return ((void*)0); } int main() { pthread_t id; int ret; ret = pthread_create(&id,NULL,threadFunc,(void *)sockClient); if (ret != 0) { printf("Create thread error!\r\n"); exit(1); } pthread_create(&thread, NULL, threadFunc, NULL); while (1) { if( kbhit() )//有键盘输入 kbhit !0; { ch = getch(); if( ch == 's' ) { pthread_suspend(); } if(ch == 'r') { pthread_resume(); } } } return 0; }
相关文章推荐
- Java多线程——线程操作
- php中的多种界面跳转方式
- 游戏中提升效率!
- 几个重要库函数的实现-strcpy,strncpy,memcpy,memset
- 自动跳转
- 谈谈网站防盗链
- Java基本数据类型对象包装类
- 关于JavaScript的闭包(closure)
- 两套解决方案
- ADO.NET查询表数据
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
- Android之使用Android-query框架进行开发(一)
- c++第五周项目二分数类
- JCS的学习与应用二:cache.ccf配置说明
- Android四大组件之BroadcastReceiver
- 批量导入表数据
- WIND2003 安装Zend studio 报错
- 自定义ECSHOP后台订单列表的商品缩略图尺寸
- 监控Cacti+Nagios+NagiosQl+NagVis+Pnp4nagios整合 推荐
- 栈于队列