简体   繁体   English

Linux C ++线程已死,但“挂起”-线程限制

[英]Linux C++ threads are dead, but “hanging” - thread limit

A friend of mine is trying to fix up a custom http server in C++ written for windows to work in Linux. 我的一个朋友正在尝试用C ++修复为Windows在Linux下工作而编写的自定义http服务器。 I tried to help him, but all I found seemed too obvious. 我试图帮助他,但发现的一切似乎太明显了。

The app creates a thread for every time a request comes in. The thread serves the request and ends. 该应用会在每次请求进入时创建一个线程。该线程为请求提供服务并结束。 After some number of requests (something over 300) new threads are not created anymore. 经过一定数量的请求(超过300个)后,不再创建新线程。

All I found is that there is a limit of threads that can be created. 我发现的全部是,可以创建的线程数量有限。 But it looks like finished threads are still there. 但是看起来成品线程仍然存在。 Is that a problem with the code or do the thread handlers never get released? 这是代码的问题,还是线程处理程序永远不会被释放?

This is a bit of code my friend extracted from the app: 这是我的朋友从应用程序中提取的一些代码:

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;
}

Looks like you've got a bunch of "joinable" threads. 看起来您有一堆“可连接”线程。 They're waiting for someone to call pthread_join() on them. 他们正在等待有人在他们身上调用pthread_join()。 If you don't want to do that (to get the return value of the thread, for example), you can create the threads as '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;
}

完成后必须加入线程( pthread_join ,另请参见wait(2) ),或者将SIGCHILD处理程序设置为SIG_IGN。

I would recommend your friend to create a thread pool, and post work items to the thread pool. 我建议您的朋友创建一个线程池,并将工作项发布到线程池中。 If there are available threads to service the work item, one thread is allocated, and given the work item. 如果有可用的线程服务该工作项,则分配一个线程,并为其分配工作项。 If no threads are available, the request should hang until a new thread is available. 如果没有可用线程,则请求应挂起,直到有新线程可用。 This way he can protect himself from draining too much system resources during a denial of service attack. 这样,他可以防止拒绝服务攻击期间自己消耗过多的系统资源。

If it is unlikely that no more than 30 requests will be handled at any one time, then the thread pool should contain at the most 30 threads. 如果一次不可能处理不超过30个请求,则线程池最多应包含30个线程。 The system resource allocation would then be predictable during a denial of service attack, and not depend on an arbitrary system limit. 这样,在拒绝服务攻击期间,系统资源分配将是可预测的,而不依赖于任意系统限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM