免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5329 | 回复: 5
打印 上一主题 下一主题

在linux下如可以随意控制一个线程阻塞,运行。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-06-30 16:59 |只看该作者 |倒序浏览
如有一个线程:当一个条件变量满足时,它开始运行,在运行中,如果另一个条件变量满足时,它又必须阻塞,
要一个条件变量还是两个,有其它的方法也可以!

其实我最主要是目的就是实现类似守护进程的功能,当没有别的线程运行时,它就运行,一旦别的线程条件满足可以运行时,它必须变为阻塞。

论坛徽章:
0
2 [报告]
发表于 2005-07-01 09:39 |只看该作者

在linux下如可以随意控制一个线程阻塞,运行。

我的问题是不是发错了版块啊,怎么没有人回复啊

论坛徽章:
0
3 [报告]
发表于 2005-07-01 09:43 |只看该作者

在linux下如可以随意控制一个线程阻塞,运行。

可以用几个线程锁来帮助线程互相协作阿

论坛徽章:
0
4 [报告]
发表于 2005-07-07 11:33 |只看该作者

在linux下如可以随意控制一个线程阻塞,运行。

man pthread_cond_wait

论坛徽章:
0
5 [报告]
发表于 2011-09-26 15:10 |只看该作者
楼主,后来你是怎么做到了,麻烦指点一下小弟!!

论坛徽章:
0
6 [报告]
发表于 2011-09-26 15:30 |只看该作者
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>


  4. int send_tag = 0;
  5. int recv_tag = 0;
  6. pthread_mutex_t ping_lock;
  7. pthread_cond_t send_cond;
  8. pthread_cond_t recv_cond;

  9. void* th1(void *arg)
  10. {
  11.         while (1)
  12.         {
  13.                 pthread_mutex_lock(&ping_lock);
  14.                 recv_tag = 0;
  15.                 sleep(1);
  16.                 printf("th1\n");
  17.                 recv_tag = 1;
  18.                 pthread_cond_signal(&recv_cond);
  19.                 while(send_tag == 0){
  20.                         pthread_cond_wait(&send_cond, &ping_lock);
  21.                 }
  22.                 send_tag = 0;
  23.                 pthread_mutex_unlock(&ping_lock);

  24.         }
  25.         return NULL;
  26. }
  27. void* th2(void *arg)
  28. {
  29.         pthread_t pid = pthread_self();
  30.         while(1)
  31.         {
  32.                 pthread_mutex_lock(&ping_lock);
  33.                 while(recv_tag == 0){
  34.                         pthread_cond_wait(&recv_cond, &ping_lock);
  35.                 }
  36.                 recv_tag = 0;
  37.                 printf("th %ld\n", pid);
  38.                 sleep(1);
  39.                 send_tag = 1;
  40.                 pthread_cond_signal(&send_cond);
  41.                 pthread_mutex_unlock(&ping_lock);
  42.         }
  43.         return NULL;
  44. }
  45. int main()
  46. {
  47.         int i = 0;
  48.         pthread_t pid;
  49.         pthread_attr_t attr;
  50.         (void) pthread_attr_init(&attr);
  51.         (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  52.         (void)pthread_mutex_init(&ping_lock, NULL);
  53.         (void)pthread_cond_init(&send_cond, NULL);
  54.         (void)pthread_cond_init(&recv_cond, NULL);
  55.         pthread_create(&pid, &attr, th1, NULL);
  56.         for(i = 0; i< 100000; i++){
  57.                 pthread_create(&pid, &attr, th2, NULL);
  58.         }

  59.         while(1);
  60.         sleep(1);
  61.         return 0;
  62. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP