工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 4035|回复: 13

当主线程退出的时候这个主线程创建的线程原来是会被回收清掉的?

[复制链接]
发表于 2008-5-11 11:30 | 显示全部楼层 |阅读模式
不知道这个结论对不对,昨天在写单元测试代码的时候有线程相关的一个测试的东西,然后隔壁的问了一下这个问题,当主线程退出的时候,这个进程的其他线程还会继续跑吗?

系统是Redhat as 4.0

#include <pthread.h>
#include <stdio.h>
/*Prints x ’s to stderr.The parameter is unused.Does not return.*/
void*print_xs (void*unused)
{
    while (1)
    {
        fputc (‘x ’,stderr);
    }
    return NULL;
}
/*The main program.*/
int main ()
{
    pthread_t thread_id;
    /*Create a new thread.The new thread will run the print_xs
    function.*/
    pthread_create (&thread_id,NULL,&print_xs,NULL);
    /*Print o ’s continuously to stderr.*/
    while (1)
    {
        fputc (‘o ’,stderr);
    }
    return 0;
}


如上这个程序,这样跑是没有问题的,创建的线程是不会被回收掉的,但是把main 函数改成

/*The main program.*/
int main ()
{
    pthread_t thread_id;
    /*Create a new thread.The new thread will run the print_xs
    function.*/
    pthread_create (&thread_id,NULL,&print_xs,NULL);
    /*Print o ’s continuously to stderr.*/

   return 0;
}


这样的话就直接执行完线程创建然后退出之后创建的这个线程也被清掉回收了.
不是很了解这个线程库的处理,有人能解释一下吗?
发表于 2008-5-11 13:10 | 显示全部楼层
#include <pthread.h>
#include <stdio.h>
/*Prints x ’s to stderr.The parameter is unused.Does not return.*/
void *other_pthread(void* unused){
           while(1){
         fputs("sub sub thread\n",stderr);     
           }
           return NULL;
}
void *print_xs (void* unused)
{
        pthread_create(&thread_id2,NULL,&other_pthread,NULL);
        int i=0;
    for(;i<100;++i)
    {
        printf("xx: %d\n",i);//,stderr);
    }
    return NULL;
}
/*The main program.*/
int main ()
{
    //pthread_t thread_id;
    /*Create a new thread.The new thread will run the print_xs
    function.*/
    pthread_t thread_id1;
        pthread_t thread_id2;
    pthread_create(&thread_id1,NULL,&print_xs,NULL);
    //pthread_create (&thread_id,NULL,&print_xs,NULL);
    /*Print o ’s continuously to stderr.*/
    while (1)
    {
        //fputc ('o',stderr);
    }
    return 0;
}


被main创建的线程死掉后其创建的线程继续运行,
会不会是main结束了其实是整个进程结束 了?
回复

使用道具 举报

发表于 2008-5-11 13:20 | 显示全部楼层
整个程序都结束了,你的线程当然挂啦

你试一下在另一个函数里面启动线程,然后结束。但保持main不结束,会发现线程还在,一直到你的main结束了
回复

使用道具 举报

发表于 2008-5-11 13:21 | 显示全部楼层
如果要在main退出后还运行,可以用fork,创建一个新的进程
回复

使用道具 举报

发表于 2008-5-11 14:25 | 显示全部楼层
在main里用pthread_join等待其它线程结束主线程才结束.
回复

使用道具 举报

发表于 2008-5-11 14:52 | 显示全部楼层
默认属性就是 pthread_join
回复

使用道具 举报

 楼主| 发表于 2008-5-11 15:02 | 显示全部楼层
是的,现在已知的就是,我main退出了就系统就把创建的线程给清掉了,那么系统是怎么做这么一个操作的?
如何实现的呢?
回复

使用道具 举报

发表于 2008-5-11 15:05 | 显示全部楼层
这个……找下执行main函数的系统函数exec函数族来看下……剩下的我看就是操作系统原理的东东了
回复

使用道具 举报

 楼主| 发表于 2008-5-11 15:16 | 显示全部楼层
其实是一个好奇了,昨天写单元测试代码的时候的要模拟这个东西
所以想着找找资料看看.
回复

使用道具 举报

发表于 2008-5-11 16:13 | 显示全部楼层
5,6F好暴力。。。
默认属性就是 pthread_join

默认属性?不明....继续看书...

hjack说的应该是指把当前线程放进队列里吧?
pthread_join(thread_id2,NULL);


PS:介绍一本书: UNIX环境高级编程
回复

使用道具 举报

发表于 2008-5-11 16:24 | 显示全部楼层
pthread_CREATE_JOINABLE

pthread_CREATE_DETACH

哦,抱歉,我意思上讲上面这两个属性值。又有几个月没碰这些啦。哈哈。
回复

使用道具 举报

 楼主| 发表于 2008-5-14 00:21 | 显示全部楼层
unix 环境高级编程?

找找电子书先,可是这些只是作为工具书的公司那边有人放着有,自己不想买...
回复

使用道具 举报

发表于 2008-5-14 12:27 | 显示全部楼层
the POSIX pthread_create automatically makes the thread runnable without requiring a separate start operation.

The pthread_join function causes the caller to wait for the specified thread to exit, similar to waitpid at the process level. To prevent memory leaks, long-running programs should eventually call either pthread_detach or pthread_join for every thread.


PS:我有电子书。

[ 本帖最后由 hjack 于 2008-5-14 12:29 编辑 ]
回复

使用道具 举报

发表于 2008-5-14 13:11 | 显示全部楼层
我借中文纸质书
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-17 04:55

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表