免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: zydzmapx

加急跪求帮助:线程资源释放的问题 [复制链接]

论坛徽章:
0
发表于 2008-08-21 23:30 |显示全部楼层
原帖由 zydzmapx 于 2008-8-21 22:28 发表



能否提供一个使用上述函数的小例子,谢谢

见axel中:

  1. int oldstate;
  2.        
  3.         /* Allow this thread to be killed at any time.                        */
  4.         pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate );
  5.         pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate );
  6.         pthread_create(setup_thread·······)
  7. pthread_cancel( &setup_thread);
复制代码

[ 本帖最后由 贺兰云天 于 2008-8-21 23:31 编辑 ]

论坛徽章:
0
发表于 2008-08-22 09:44 |显示全部楼层
原帖由 duanjigang 于 2008-8-21 21:19 发表
不想顺着楼主的想法去想怎么做,我觉得你的起初的思路有问题,即使有人帮你解决了这个问题,估计你的饭碗会丢得很快了
================================
为什么不改一下你的设计,我以前做过跟你这 ...

我支持8楼的做法这样去管理一个线程池的模式在实际应用中远远好于你的设计思路

论坛徽章:
0
发表于 2008-08-22 09:46 |显示全部楼层

好像还是不能释放资源

为了验证大家的方法,我作料一个小的测试程序如下(由于是从别的例子程序改造过来的,存在部分程序啰嗦,请别追究,谢谢,请重点关注线程释放部分的 程序)
# include <stdio.h>
# include <errno.h>
# include <pthread.h>
# include <unistd.h>
# define THREAD_NUM 2


void *my_thread(void *arg)
{
        pthread_t id;
        int *nump;
        int num;
        int retval;

        nump=(int *)(arg);
        num=*nump;
        printf("thread %d:started\n",num);
        id=pthread_self();
        printf("thread %d:ID is %d\n",num,id);
        int k =0;
        //等2秒退出线程
        while(k<2)
        {
                k++;
                usleep(1000000);
        }
        retval=100+num;
        printf("thread %d:exit with %d\n",num,retval);
        pthread_cancel(id) ;
        //pthread_exit(&retval);
        //////////////BBB处
       
}

int main()
{
        int count;
        pthread_t thread[THREAD_NUM+1];
        int *retval[THREAD_NUM+1];
        int arg[THREAD_NUM+1];

        count=1;
        printf("Create thread %d\n",count);
        arg[count]=count;

        pthread_attr_t attr;
        pthread_attr_init( &attr);
        pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
       
        int oldstate;
//        pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate );
//        pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate );
        pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL);
        pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS,NULL );
        if(pthread_create(&thread[count],&attr,my_thread,(void*)(&arg[count]))!=0)
                fprintf(stderr,"Count not create thread %d:%s\n",count,strerror(errno));
        //if(pthread_join(thread[count],(void **)(&retval[count]))!=0)
        //        fprintf(stderr,"No thread %d to join:%s\n",        count,strerror(errno));
        //else
        //        printf("Thread %d retuned %d\n",count,*retval[count]);

        count++;
        //////////////AAA处
        int k =0;
        //等10秒退出主进程
        while(k<10)
        {
                k++;
                usleep(1000000);
        }               

        exit(0);
}



程序运行结果是:

程序运行到//////////////BBB处时线程资源没有释放,只有等程序等了10秒后退出主进程才释放了线程资源,而且有时还没完全释放,请教一下大家,为什么呀?

我把例子程序发到附件中,请大家一定帮个忙指点一下

论坛徽章:
0
发表于 2008-08-22 09:54 |显示全部楼层
我建议你好好看看权威一些的书.

Advanced Programming in the UNIX® Environment: Second Edition
现在有third edition.

里面有详细的,线程的产生,结束,属性的修改.

pthread_cancel 好像是用于cancel同一进程中另外的线程的. 反正我没有用过来cancel自己.

而且线程自己退出是用pthread_exit,也可以直接就return了.



ps:
有时候慢慢来,就是最好的方法.

论坛徽章:
0
发表于 2008-08-22 10:03 |显示全部楼层
写个最简单的方法吧,你测试下可以释放不。
void do()
{
      pthread_detach(pthread_self());
      dosomething;
       return;
}
main()
{
     pthread_t tid;
     pthread_create(&tid, NULL, do, NULL);
     dosomething;
     exit(0);
}

论坛徽章:
0
发表于 2008-08-22 10:06 |显示全部楼层
原帖由 zydzmapx 于 2008-8-22 09:46 发表
为了验证大家的方法,我作料一个小的测试程序如下(由于是从别的例子程序改造过来的,存在部分程序啰嗦,请别追究,谢谢,请重点关注线程释放部分的 程序)
# include
# include
# include
# include
#  ...

楼主, 你看看下面的代码吧, 希望对你有用:


  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<sys/types.h>

  6. void* thr_fn(void* args)
  7. {
  8.                 while (1)
  9.                 {
  10.                                 sleep(1);
  11.                                 printf("my pid is %d\n", getpid());
  12.                 }

  13. }

  14. int main()
  15. {
  16.                 pthread_t tid;
  17.                 pid_t pid;
  18.                 printf("pid=%d\n", getpid());
  19.                 pthread_create(&tid, NULL, thr_fn, NULL);
  20.                 sleep(5);
  21.                 pthread_cancel(tid);
  22.                 while (1)
  23.                 {
  24.                                 sleep(2);
  25.                                 printf("in parent:tid=%u\n", tid);
  26.                 }
  27.                 return 0;
  28. }

复制代码

论坛徽章:
0
发表于 2008-08-22 10:40 |显示全部楼层
原帖由 scutan 于 2008-8-22 10:06 发表

楼主, 你看看下面的代码吧, 希望对你有用:


#include
#include
#include
#include
#include

void* thr_fn(void* args)
{
                while (1)
                {
                      ...




首先谢谢您,我按你的程序编译运行结果如下:
运行前剩余内存:81004  ,运行后内存一直是80900, 中途经过多个"my pid id 294",再不断的显示int parent :tid=1026,但剩余内存一直没有任何变化

不知这个程序在您那是否是可以的吗?我用的moxa的嵌入式开发平台,不应该是设备及系统的问题吧

论坛徽章:
0
发表于 2008-08-22 11:07 |显示全部楼层
如何LZ要很执着地使用pthread_cancel.建议去看一下
http://www.ibm.com/developerwork ... ix_threadapi/part1/

论坛徽章:
0
发表于 2008-08-24 00:17 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2008-08-24 17:40 |显示全部楼层
在经历了2个星期的各种尝试和改造后,依然没有解决我想象中的动态创建和关闭线程的资源释放的问题,在此郑重的向各位好心人表示真心的感谢,最后采纳了duanjigang和 贺兰云天 的意见,即客户端连接线程创建不释放的方法,虽然程序上看似不理想,但解决了我每次连接端开时损失的几十K的资源,谢谢大家

[ 本帖最后由 zydzmapx 于 2008-8-24 17:44 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP