c++ - 一段时间后创建线程失败

标签 c++ linux multithreading

我在 Linux C++ 程序中编写了一个使用线程的代码。但是过了一会儿就失败了,我不知道为什么。我认为某处可能存在内存泄漏。这是一个简化版本:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>

using namespace std;


#define MAX_THREADS  20
#define THREAD_STACK  100000
pthread_t pid[MAX_THREADS];

unsigned thread_args[MAX_THREADS][2];
volatile unsigned thread_number = 0;

void* TaskCode(void* parg)
{
    unsigned a = ((unsigned *)parg)[0];
    unsigned b = ((unsigned *)parg)[1];

    for(int i = 0; i < 1000; i++)
        ;
    cout<< "\n\n" << a << "  " << b << "\n\n";

    thread_number--;
    return 0;
}

void Action(unsigned long a,unsigned b)
{
    if(thread_number >= MAX_THREADS)
        return;
    pthread_attr_t  attrs;
    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, THREAD_STACK);
    thread_args[thread_number][0] = a;
    thread_args[thread_number][1] = b;
    if(pthread_create(&pid[thread_number],&attrs, TaskCode, (void*) thread_args[thread_number]) != 0)
    {
        cout<< "\n\n" "new thread failed. thread number:" << thread_number << "\n\n";
        for(unsigned i = 0; i < thread_number; i++)
            pthread_kill(pid[i], SIGSTOP);
    }
    thread_number++;
}

int main()
{
    int a = 0;
    while(true)
    {
        for(int i = 0; i < 1000; i++)
            ;
        Action(time(0),1);
    }

    cout<< "\n\nunexpected end\n\n";
}

这是怎么回事?


编辑: 按照建议我更改了代码:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>

using namespace std;

#define MAX_THREADS  20
#define THREAD_STACK  100000
pthread_t pid[MAX_THREADS];

unsigned thread_args[MAX_THREADS][2];
volatile unsigned thread_number = 0;

pthread_mutex_t mutex_;

void* TaskCode(void* parg)
{
    unsigned a = ((unsigned *)parg)[0];
    unsigned b = ((unsigned *)parg)[1];

    for(int i = 0; i < 1000; i++)
        ;
    cout<< "\n\n" << a << "  " << b << "\n\n";
    pthread_mutex_lock(&mutex_);
    thread_number--;
    pthread_mutex_unlock(&mutex_);
    return 0;
}

void Action(unsigned long a,unsigned b)
{
    if(thread_number >= MAX_THREADS)
        return;
    pthread_attr_t  attrs;
    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, THREAD_STACK);
    thread_args[thread_number][0] = a;
    thread_args[thread_number][1] = b;
    if(pthread_create(&pid[thread_number],&attrs, TaskCode, (void*) thread_args[thread_number]) != 0)
    {
        cout<< "\n\n" "new thread failed. thread number:" << thread_number << "\n\n";
        for(unsigned i = 0; i < thread_number; i++)
            pthread_kill(pid[i], SIGSTOP);
    }
    pthread_mutex_lock(&mutex_);
    thread_number++;
    pthread_mutex_unlock(&mutex_);
}

int main()
{
    int a = 0;
    pthread_mutex_init(&mutex_, NULL);
    while(true)
    {
        for(int i = 0; i < 1000; i++)
            ;
        Action(time(0),1);
    }
    pthread_mutex_destroy(&mutex_);
    cout<< "\n\nunexpected endn\n";
}

还是失败了。

最佳答案

默认情况下创建线程时 detachstate 设置为 joinable。这就需要加入线程,获取线程的返回码,清理使用的资源。

如果不进行清理,您将很快耗尽内存(内存泄漏)。

对于线程不需要返回任何代码并且可以在线程退出时立即清理内存的情况,然后可以创建分离线程。

要设置属性以便所有创建的线程都将分离,请使用 pthread_attr_setdetachstate()。将值设置为 PTHREAD_CREATE_DETACHED

pthread_init(&attrs);
pthread_attr_setcreatedetached(&attrs,PTHREAD_CREATE_DETACHED);

关于c++ - 一段时间后创建线程失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798068/

相关文章:

c++ - 创建一个可以接收 board1[{1,1}] ='X' 的类; ? (方括号内的大括号)

c++ - 在 Linux 中使用 C++ 库 (eclipse)

linux - 删除 Unix 中行字段之间的空格

java - 从异步线程访问Android Activity 堆栈?

c++ - 从参数包中排除前 n 个参数

c++ - Copy Constructor C++编译错误

c++ - 如何在任何类中使用 Objects form main? C++

linux - Python 子进程可以在没有 shell = true 的情况下创建 conda 环境吗?

java - 将值从辅助线程传递到主线程

java - 在 App Engine Java 开发服务器中,如何模拟并发线程以确保安全?