免费注册 查看新帖 |

Chinaunix

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

C语言多线程编程时,线程号没有释放,直到主进程退出或程序出错. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-14 17:00 |只看该作者 |倒序浏览
我的程序和运行结果

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include<pthread.h> /* Linux线程库头文件 */

void * start(void * tempc);
int main()
{
        pthread_t tid1, tid2;
        /* 创建两个线程 */
        //EXEC SQL ENABLE THREADS;
        pthread_attr_t attr;
        /*初始化属性值,均设为默认值*/
        pthread_attr_init(&attr);
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
       
        while(1)
        {
                static int i=2;
                if( pthread_create( &tid1,&attr,&start,NULL ) )
                {
                        printf( \"创建线程失败!thread ID1:%d,Err No:%d,%s\\n\",tid1,errno,strerror(errno) );
                        i--;
                        if (i==0) exit(1);
                }
                else
                {
                        //close(tid1);
                        //printf(\"thread 1 finished,tid1:%l\\n\",tid1);
                        //pthread_join( tid1,NULL );
                }
                //pthread_detach(tid1);
                /* 等待线程退出 */
                //if( pthread_join( tid1,NULL ) )
                //{
                //        printf( \"等待线程结束失败!\\n\" );
                //        exit(1);
                //}
                if( pthread_create( &tid2,NULL,start,NULL ) )
                {
                printf( \"创建线程失败!thread ID2:%d,Err No:%d,%s\\n\",tid2,errno,strerror(errno) );
                        i--;
                        if (i==0) exit(1);
                }
                else
                {
                        //printf(\" thread 2 finished,tid2:%ul\\n\",tid2);
                }
        }
        exit(0);
}

void * start(void *tempc)
{
        int oldstate;
        pthread_detach(pthread_self());
        //fprintf(stderr,\"Starting second child tid=%d\\n\",pthread_self());
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate); /*Enable cancellation again*/;
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldstate);
        for(int i=0;i++;i<=100000);
        if(unsigned long THcnt=(pthread_self()%100000) ==0)
        printf(\"thread:%u\\n\",pthread_self());
        //sleep(1);
        pthread_cancel(0);
        pthread_exit(0);
        return 0;
}




$ uname -a
HP-UX hostname01 B.11.11 U 9000/800 987654321 unlimited-user license
$ ./mth
thread:100000
thread:200000
thread:300000
thread:400000
...........
thread:2800000
创建线程失败!thread ID2:2896321,Err No:2,No such file or directory
创建线程失败!thread ID2:2896323,Err No:2,No such file or directory

[ 本帖最后由 microyong 于 2008-1-16 12:43 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-01-23 18:50 |只看该作者
1) 线程号是循环利用的,不存在释放不释放的问题。你用了pthread_detach()后在线程函数return后资源会自动释放,不用担心。那些pthread_cancel()等等都是不必要的
2) pthread系列函数返回值就是错误号,而不是如一般UNIX系统调用(准确的说,是系统调用的C库包装)一样设置errno变量。
3) 我个人估计错误应该你的程序里pthread_create()返回的错误应该是EAGAIN,此时你可以sleep适当的时间再continue即可
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP