多线程以及内存释放问题?

yangzhifu 2010-12-28 10:26:56
说一下整个设计思想:
------->service_thread------->timer_thread
|
|
|
main_thread- |
|
|
|------->service_thread------->timer_thread

主线程在accept后调用service_thread接收网络数据,其中的数据有心跳包,所以由service_thread启动了timer_thread,service_thread会等待timer_thread结束后结束,但是发现每次链接过后会多分配4k的内存,是否需要再在线程里面调用pthread_exit函数,但是有人说这个是一种自杀的方式,还有我跟踪了里面的变量内存申请以及释放都是作完了的,看我还有那里没有考虑到,请大家想想,下面是我top的结果,红色部分为增加部分:
Mem: 40820K used, 78052K free, 0K shrd, 8000K buff, 6660K cached
Load average: 0.03, 0.02, 0.00 (State: S=sleeping R=running, W=waiting)

PID USER STATUS RSS PPID %CPU %MEM COMMAND
1150 root R 780 889 4.7 0.6 top
1100 root S < 6824 891 0.9 5.7 test
902 root S < 6824 891 0.0 5.7 test
901 root S < 6824 891 0.0 5.7 test
894 root S < 6824 891 0.0 5.7 test
887 root S < 6824 1 0.0 5.7 test
904 root S < 6824 891 0.0 5.7 test
891 root S < 6824 887 0.0 5.7 test
...全文
256 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
justkk 2010-12-28
  • 打赏
  • 举报
回复
timer_thread的作用是什么?
它调用return 和 pthread_exit 的效果应该一样吧
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
还有个main_thread没有pthread_join等待service_thread结束,而service_thread等待了timer_thread的结束
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
PID USER STATUS RSS PPID %CPU %MEM COMMAND
1150 root R 780 889 4.7 0.6 top
1100 root S < 6824 891 0.9 5.7 encode
902 root S < 6824 891 0.0 5.7 encode
901 root S < 6824 891 0.0 5.7 encode
894 root S < 6824 891 0.0 5.7 encode
887 root S < 6824 1 0.0 5.7 encode
904 root S < 6824 891 0.0 5.7 encode
891 root S < 6824 887 0.0 5.7 encode
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 justkk 的回复:]

试验一下吧
调用pthread_join(),看看内存情况..
[/Quote]

不解,为什么线程分离没有问题,但是等待结束的pthread_join()却可以
justkk 2010-12-28
  • 打赏
  • 举报
回复
试验一下吧
调用pthread_join(),看看内存情况..
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 justkk 的回复:]

"主线程不等待其他线程结束会引起内存增加吗?"

有这个可能
如果不想等待,试试用pthread_detach() 将线程分离
[/Quote]

还是一样会漏内存
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 justkk 的回复:]

"主线程不等待其他线程结束会引起内存增加吗?"

有这个可能
如果不想等待,试试用pthread_detach() 将线程分离
[/Quote]

对,可以试试
justkk 2010-12-28
  • 打赏
  • 举报
回复
"主线程不等待其他线程结束会引起内存增加吗?"

有这个可能
如果不想等待,试试用pthread_detach() 将线程分离
yangzhifu 2010-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 justkk 的回复:]

timer_thread的作用是什么?
它调用return 和 pthread_exit 的效果应该一样吧
[/Quote]

timer_thread的作用就是在接受到心跳包后判断现在网络是否断线,这个线程可以不管,它可以正常结束,那就是main_thread没有等待service_thread结束了...

主线程不等待其他线程结束会引起内存增加吗?

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧