代码之家  ›  专栏  ›  技术社区  ›  naugtur

Linux C++线程已经死了,但是“挂”线程限制

  •  0
  • naugtur  · 技术社区  · 14 年前

    我的一个朋友正在尝试为Windows编写一个定制的HTTP服务器,用于Windows工作在Linux中。我试图帮助他,但我所发现的似乎太明显了。

    应用程序每次请求进入时都会创建一个线程。线程服务于请求并结束。在一些请求(大约超过300个)之后,就不再创建新的线程了。

    我发现有一个线程限制可以创建。但看起来完成的线程仍然存在。这是代码的问题还是线程处理程序从未被释放?

    这是我朋友从应用程序中提取的一点代码:

    pthread_t threadID;
    
    StartingArgs *arg = new StartingArgs( &(this->cameraCounts), mapToSend,&(this->mapMutex), &(this->mutex), this->config );
    
    if( pthread_create(&threadID, NULL, (this->startingRoutine) , (void*)arg ) != 0 )
        {
            ConsoleMessages::printDate();
            cout<< "snapshot maker: new thread creation failed\n";
        }
    
    void *CameraCounter::startingRoutine( void *arg )
    {
    //stuff to do. removed for debugging
    
        delete realArgs;
        return NULL;
    }
    
    3 回复  |  直到 14 年前
        1
  •  7
  •   Harper Shelby damiankolasa    14 年前

    pthread_t threadID;
    pthread_attr_t attrib;
    
    pthread_attr_init(&attrib); 
    pthread_attr_setdetachstate(pthread_attr_t &attrib, PTHREAD_CREATE_DETACHED);
    
    StartingArgs *arg = new StartingArgs( &(this->cameraCounts), mapToSend,&(this->mapMutex), &(this->mutex), this->config );
    
    if( pthread_create(&threadID, &attrib, (this->startingRoutine) , (void*)arg ) != 0 )
    {
            ConsoleMessages::printDate();
            cout<< "snapshot maker: new thread creation failed\n";
    }
    
    pthread_attr_destroy(&attrib);
    
    void *CameraCounter::startingRoutine( void *arg )
    {
    //stuff to do. removed for debugging
    
        delete realArgs;
        return NULL;
    }
    
        2
  •  2
  •   smilingthax    14 年前

    pthread_join wait(2)

        3
  •  2
  •   Jörgen Sigvardsson    14 年前