c++ - Linux C++ 线程已死,但 "hanging"- 线程限制

标签 c++ multithreading limit

我的一个 friend 正在尝试用 C++ 修复一个为 Windows 编写的自定义 http 服务器,以便在 Linux 中运行。我试图帮助他,但我发现的一切似乎都太明显了。

应用程序会在每次收到请求时创建一个线程。该线程处理请求并结束。在一定数量的请求(超过 300 个)之后,不再创建新线程。

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

这是我的 friend 从应用程序中提取的一些代码:

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

最佳答案

看起来你有一堆“可连接”的线程。他们正在等待有人对他们调用 pthread_join() 。如果您不想这样做(例如,为了获取线程的返回值),您可以将线程创建为“分离的”:

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

关于c++ - Linux C++ 线程已死,但 "hanging"- 线程限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001370/

相关文章:

ios - Parse.com 查询限制 - 影响 whereKey 限制?

c++ - 我使用 AudioKit 使用什么语言?

c++ - 使用 boost hana 从类型列表中删除重复项的统一方法

c++ - 如何在 C++ 中使用 scanf() 读取字符串?

java - 修复了大小为 1 的线程池并使其连续 <- 它们相同吗?

sql - 如何限制为Interbase 7.1返回的记录数?

c++ - Windows Shell Extension 不提供确切的文件路径

java - 在线程上调用不同的方法

c++ - 确保仅从对象的创建线程调用方法

MySQL 使用 LIMIT 返回第一条记录