使线程在一段时间后失败 [英] Making threads fails after a while

查看:116
本文介绍了使线程在一段时间后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a code which uses threads in a Linux C++ program. But it fails after a while, I don't know why. I think there may be a memory leakage somewhere. This is a simplified version:

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

它有什么问题?

编辑
根据建议我更改了代码:

As suggested I changed the code:

#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来创建线程。这需要线程被连接以获得线程的返回代码并清理所使用的资源。

By default threads are created with detachstate set to joinable. This requires that threads be joined to get the return code of a thread and cleanup the resources used.

没有清理你会很快耗尽内存(内存泄漏)。

Without cleaning up you'll run out of memory really quickly (memory leak).

对于线程不需要返回任何代码和清除内存的情况,只要线程退出,就可以创建分离的线程。

For cases where threads need not return any code and cleanup of memory can be done as soon as the thread exits, then detached threads can be created.

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

To set the attribute such that all threads created will be detached use pthread_attr_setdetachstate(). Set the value to PTHREAD_CREATE_DETACHED.

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

这篇关于使线程在一段时间后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆