循环pthread_create导致虚拟内存上涨
2010-08-29 20:14
591 查看
代码
编译运行后查看虚拟内存上涨,导致这个原因是因为pthread_create创建线程太快,而且每个线程运行时间都教长,因此循环创建线程都需要注意这个问题,现在的解决方法是在pthread_create创建线程之后添加usleep()使其休眠一段时间,具体时间可以使用算法动态修改,也可以确定一个定值
//测试pthread_create创建太快导致虚拟内存一直上涨直至上限 //pthread_create_test.c #include <pthread.h> #include <stdio.h> #include <sys/wait.h> #define WAITTIME 500000 void * Client_TS_handle_ss_local_data_cmd(void * arg) { printf("enter local ss handler\n"); printf("leave local ss handler\n"); usleep(WAITTIME); pthread_detach(pthread_self()); pthread_exit(NULL); } void * Client_TS_handle_up_local_data_cmd(void * arg) { printf("enter local up handler\n"); printf("leave local up handler\n"); usleep(WAITTIME); pthread_detach(pthread_self()); pthread_exit(NULL); } int main() { pthread_t _local_up_thread; pthread_t _local_ss_thread; while(1) { if(pthread_create(&_local_up_thread,NULL,Client_TS_handle_up_local_data_cmd,NULL)!=0) { perror("Error Creating UP Thread"); } usleep(1000); if(pthread_create(&_local_ss_thread,NULL,Client_TS_handle_ss_local_data_cmd,NULL)!=0) { perror("Error Creating SS Thread"); } } return 0; }
编译运行后查看虚拟内存上涨,导致这个原因是因为pthread_create创建线程太快,而且每个线程运行时间都教长,因此循环创建线程都需要注意这个问题,现在的解决方法是在pthread_create创建线程之后添加usleep()使其休眠一段时间,具体时间可以使用算法动态修改,也可以确定一个定值
相关文章推荐
- 循环pthread_create导致虚拟内存上涨(续1)
- 循环pthread_create导致虚拟内存上涨(续2)
- 解决了一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- 解决了一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- pthread_create后没有detach导致内存持续增长
- 一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- pthread_create后没有detach导致内存持续增长
- pthread_jion() , pthread_create() 导致的segmentation fault
- 隐蔽的内存泄漏——pthread_create 属性设置不当导致
- pthread_jion() , pthread_create() 导致的segmentation fault
- Linux-pthread_create使用不当可能导致内存泄露
- pthread_create导致的程序崩溃
- pthread_create 参数传递指针问题(创建多线程时,循环变量的传递)
- 隐蔽的内存泄漏——pthread_create 属性设置不当导致
- 解决了一个隐蔽的内存泄漏——pthread_create后没有detach导致内存持续增长
- 隐蔽的内存泄漏——pthread_create 属性设置不当导致
- pthread_create 导致内存泄露
- C++ 中使用pthread_create的问题
- c++test使用qt中的 mingw 创建pthread_create程序无法运行的问题
- C语言中数组越界导致死循环的测试