免费注册 查看新帖 |

Chinaunix

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

求助:线程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-10 12:30 |只看该作者 |倒序浏览
#include  <stdio.h>
#include  <pthread.h>
#include  <unistd.h>
#include  <stdlib.h>
void *thread_1(void);
void *thread_2(void);

int main(int argc, char *argv[])
{
        pthread_t pt_thread_1,pt_thread_2;
        pthread_attr_t attr;
       
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

        if (pthread_create(&pt_thread_1, &attr, (void *(*)(void *))thread_1,, NULL))
        {
                printf("pt_thread_1 error.\n");
                exit(1);
        }
       
        if (pthread_create(&pt_thread_2 &attr, (void *(*)(void *))thread_2(, NULL))
        {
                printf("pt_thread_2 error.\n");
                exit(1);
        }

        return 0;
}

void *thread_1(void)
{
        for(;;)
        {
                pthread_testcancel();
                printf("thread_1\n");
                sleep(3);
        }
       
}

void *thread_2(void)
{
        for(;;)
        {
                pthread_testcancel();
                printf("thread_2!\n");
                sleep(1);
        }
       
}
为什么运行结果是:
thread_1
thread_2
如何让他们不退出,而是在线程里无限循环呢?

[[i] 本帖最后由 zgx120106 于 2006-12-10 12:32 编辑 [/i]]

论坛徽章:
0
2 [报告]
发表于 2006-12-10 13:10 |只看该作者
main最后加pthread_join

论坛徽章:
0
3 [报告]
发表于 2006-12-10 13:17 |只看该作者
这样改还是不行,运行结果还是一样
#include  <stdio.h>
#include  <pthread.h>
#include  <unistd.h>
#include  <stdlib.h>
void *thread_1(void);
void *thread_2(void);

int main(int argc, char *argv[])
{
        pthread_t pt_thread_1,pt_thread_2;
        pthread_attr_t attr;
        
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

        if (pthread_create(&pt_thread_1, &attr, (void *(*)(void *))thread_1,, NULL))
        {
                printf("pt_thread_1 error.\n");
                exit(1);
        }
        
        if (pthread_create(&pt_thread_2 &attr, (void *(*)(void *))thread_2(, NULL))
        {
                printf("pt_thread_2 error.\n");
                exit(1);
        }
        
        pthread_join(pt_thread_1,NULL);
        pthread_join(pt_thread_2,NULL);

        return 0;
}

void *thread_1(void)
{
        for(;;)
        {
                pthread_testcancel();
                printf("thread_1\n");
                sleep(3);
        }
        
}

void *thread_2(void)
{
        for(;;)
        {
                pthread_testcancel();
                printf("thread_2!\n");
                sleep(1);
        }
        
}

[[i] 本帖最后由 zgx120106 于 2006-12-10 13:36 编辑 [/i]]

论坛徽章:
0
4 [报告]
发表于 2006-12-10 13:51 |只看该作者
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
这一行去掉

论坛徽章:
0
5 [报告]
发表于 2006-12-10 16:07 |只看该作者
你的代码创建线程那里很搞笑。

  1. -bash-2.05b$ cat test.c
  2. #include  <stdio.h>
  3. #include  <pthread.h>
  4. #include  <unistd.h>
  5. #include  <stdlib.h>
  6. void *thread_1(void);
  7. void *thread_2(void);

  8. int main(int argc, char *argv[])
  9. {
  10.         pthread_t pt_thread_1,pt_thread_2;
  11.         pthread_attr_t attr;
  12.         
  13.         if ( pthread_create(&pt_thread_1, NULL, (void *)thread_1, NULL) )
  14.         {
  15.                 printf("pt_thread_1 error.\n");
  16.                 exit(1);
  17.         }
  18.         
  19.         if ( pthread_create(&pt_thread_2, NULL, (void *)thread_2, NULL) )
  20.         {
  21.                 printf("pt_thread_2 error.\n");
  22.                 exit(1);
  23.         }

  24.         pthread_join( pt_thread_1, NULL);
  25.         pthread_join( pt_thread_2, NULL);


  26.         return 0;
  27. }

  28. void *thread_1(void)
  29. {
  30.         for(;;)
  31.         {
  32.                 printf("thread_1\n");
  33.                 sleep(3);
  34.         }
  35.         
  36. }

  37. void *thread_2(void)
  38. {
  39.         for(;;)
  40.         {
  41.                 printf("thread_2!\n");
  42.                 sleep(1);
  43.         }
  44.         
  45. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2006-12-10 16:12 |只看该作者
呵呵,我也觉得奇怪,这是照一个例子改的,但更奇怪的它却能运行

论坛徽章:
5
IT运维版块每日发帖之星
日期:2015-08-06 06:20:00IT运维版块每日发帖之星
日期:2015-08-10 06:20:00IT运维版块每日发帖之星
日期:2015-08-23 06:20:00IT运维版块每日发帖之星
日期:2015-08-24 06:20:00IT运维版块每日发帖之星
日期:2015-11-12 06:20:00
7 [报告]
发表于 2006-12-11 14:11 |只看该作者
因为你把属性设置成不等待的了。所以join 处不能停止。
1、改线程属性为NULL。
2、只开一个线程,另一个任务让原线程去做。
3、原线程加如下alarm();

论坛徽章:
0
8 [报告]
发表于 2006-12-11 17:46 |只看该作者
原帖由 zgx120106 于 2006-12-10 16:12 发表
呵呵,我也觉得奇怪,这是照一个例子改的,但更奇怪的它却能运行


-------------------------------------
你这话也有点奇怪啊,呵。用pthread 的库函数的时候,你该先查看它们的帮助啊,都不知道怎么用,怎么写程序?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP