zoukankan      html  css  js  c++  java
  • 循环pthread_create导致虚拟内存上涨

    代码
    //测试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()使其休眠一段时间,具体时间可以使用算法动态修改,也可以确定一个定值

  • 相关阅读:
    Centos7防火墙
    MySql按日期进行统计
    MySQL配置文件详解
    MySQL查看和修改表的存储引擎
    计算文件词频
    MapReduce寻找共同好友
    Centos 7 安装 memcached
    Linux常用命令(二)--文件目录命令
    Linux常用命令(一)--系统命令
    python学习笔记:(八)条件语句
  • 原文地址:https://www.cnblogs.com/eavn/p/1812040.html
Copyright © 2011-2022 走看看