linux中使用pthread_create创建线程,多线程问题, 在线等候... $
我在程序中使用pthread_create创建多线程,其中当子线程异常时,子线程退出,主线程负责重新创建子线程.这样,程序运行一段时间后,子线程再创建就不成功了!是不是我子线程退出时有些资源没释放啊?! 请诸位多指点! :) 问题点数:100、回复次数:4Top
1 楼ttapi(我爱虫虫)回复于 2004-02-03 09:57:58 得分 20
未释放资源:
pthread_join 调用了没有?
或者是用 detach 方式创建。
Top
2 楼pacman2000(pacman)(影子传说)回复于 2004-02-03 10:07:49 得分 20
觉得是没有回收。估计是到了一个进程能创建的最大线程数限制才出错的。
pthread_join比如指定要join哪个线程,所以还是detach方式,然后想办法检测或通知的好。Top
3 楼boxban(冻酸梨)回复于 2004-02-03 10:17:11 得分 60
1. 你在调用pthread_create时,是否指定了线程属性:joinable
int pthread_create(pthread_t * thread, pthread_attr_t *
attr, void * (*start_routine)(void *), void * arg);
... The attr argument can also be NULL, in which case default attributes are used: the created thread is joinable (not detached) and has default (non real-time) scheduling policy.
2. 主进程应该在pthread_create调用成功后,调用pthead_join监测子线程是否退出
void foo()
{
thread_t tid;
void* thread_ret;
int flag;
do{
flag = pthread_create(&tid, NULL, thread_proc, arg);
}while((flag == 0) && (flag = pthread_join(tid, &thread_ret)) == 0));
printf("error:%d:%s\n", flag, strerror(flag));
}
3 可以在每次重新创建线程时打印,记录线程失败的频度,以便查明原因;在线程函数内部发生异常时,打印异常消息。
Top
4 楼xiaoqingyu(小青鱼)回复于 2004-02-03 10:32:59 得分 0
谢谢各位,已解决!:)我再子线程中pthread_detach(pthread_self());这样好像可以了!Top




