I';在多次pthread_create调用后出现“无法分配内存”错误

I';在多次pthread_create调用后出现“无法分配内存”错误,c,linux,multithreading,unix,pthreads,C,Linux,Multithreading,Unix,Pthreads,我正在使用Posix线程,并编写了以下代码,其中我创建了许多线程,但为此重用pthread\u t变量: #include <pthread.h> #include <unistd.h> #include <stdio.h> // The amount of thread creation iterations static const int N = 300; static pthread_t thread_1, thread_2, thread_3;

我正在使用Posix线程,并编写了以下代码,其中我创建了许多线程,但为此重用
pthread\u t
变量:

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

// The amount of thread creation iterations
static const int N = 300;

static pthread_t thread_1, thread_2, thread_3;

void * logic_1(void * arg)
{
  usleep(1 * 1000);
  printf("logic_1 end\n");
  return 0;
}

void * logic_2(void * arg)
{
  usleep(1 * 1000);
  printf("logic_2 end\n");
  return 0;
}

void * logic_3(void * arg)
{
  usleep(1 * 1000);
  printf("logic_3 end\n");
  return 0;
}

int main()
{
    int counter = 0;
    int i;
    for(i = 0; i < N; ++i)
    {
        if (pthread_create(&thread_1, NULL, &logic_1, NULL) != 0)
        {
          perror("error: ");
          printf("thread1 did not start after %d threads that were started\n", counter);
          break;
        }
        else
        {
            ++counter;
            printf("start %d thread\n", counter);
        }
        if (pthread_create(&thread_2, NULL, &logic_2, NULL) != 0)
        {
          perror("error: ");
          printf("thread2 did not start after %d threads that were started\n", counter);
          break;
        }
        else
        {
            ++counter;
            printf("start %d thread\n", counter);
        }
        if (pthread_create(&thread_3, NULL, &logic_3, NULL) != 0)
        {
          perror("error: ");
          printf("thread3 did not start after %d threads that were started\n", counter);
          break;
        }
        else
        {
            ++counter;
            printf("start %d thread\n", counter);
        }                      
        usleep(500 * 1000);
    }
    pthread_join(thread_1, NULL);
    pthread_join(thread_2, NULL);
    pthread_join(thread_3, NULL);
    return 0;
}

你能告诉我我做错了什么吗?我认为我在linux中遇到了一些界限或限制?每次调用
返回0后,线程的资源是否会被释放logic_1
logic_2
logic_3
函数中的code>语句?也许我应该使用一个线程数组,并为此数组的每个项调用
pthread\u join
函数?

您应该在循环内部而不是外部调用pthread\u join,以便在下一次迭代中启动新的线程集之前释放分配的资源 根据pthread_join手册页,如果不加入线程,将丢失系统资源

   Failure to join with a thread that is joinable (i.e., one that is not
   detached), produces a "zombie thread".  Avoid doing this, since each
   zombie thread consumes some system resources, and when enough zombie
   threads have accumulated, it will no longer be possible to create new
   threads (or processes).

每个线程都有自己的堆栈。默认情况下,该堆栈相当大,大约为8兆字节。如果线程已分离,则当线程退出时,堆栈将被丢弃。如果线程是可连接的,则在连接线程时丢弃堆栈。默认情况下,线程是可连接的。因为您保留了创建的每个线程的堆栈,所以最终会耗尽内存。如果您想确定这一点,请创建一个函数,该函数打开
/proc/self/maps
,并使用
fgetc()
/
putchar()
循环读取和打印其内容;您将在输出中看到堆栈段。
   Failure to join with a thread that is joinable (i.e., one that is not
   detached), produces a "zombie thread".  Avoid doing this, since each
   zombie thread consumes some system resources, and when enough zombie
   threads have accumulated, it will no longer be possible to create new
   threads (or processes).