Linux C++线程已经死了,但是"挂起" - 线程限制

nau*_*tur 0 c++ multithreading limit

我的一个朋友正在尝试用C++编写的自定义http服务器来为Windows工作.我试图帮助他,但我发现的一切似乎都太明显了.

每次请求进入时,应用程序都会创建一个线程.线程服务请求并结束.在一些请求(超过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;
}
Run Code Online (Sandbox Code Playgroud)

Har*_*lby 7

看起来你有一堆"可连接"线程.他们正在等待有人在他们身上调用pthread_join().如果您不想这样做(例如,获取线程的返回值),您可以将线程创建为'detached':

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;
}
Run Code Online (Sandbox Code Playgroud)