gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:55 28 4
gpt4 key购买 nike

我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com