c - 多次 pthread_create 调用后出现 `Cannot allocate memory` 错误

标签 c linux multithreading unix pthreads

我正在玩 Posix 线程并编写了以下代码,我在其中创建了很多线程,但为此目的重用了 pthread_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;
}

执行后出现错误:

...
start 376 thread
start 377 thread
start 378 thread
logic_3 end
logic_2 end
logic_1 end
start 379 thread
start 380 thread
start 381 thread
logic_3 end
logic_2 end
logic_1 end
error: : Cannot allocate memory
thread1 did not start after 381 threads that were started

你能告诉我我做错了什么吗?我认为我在 Linux 中遇到了一些界限或限制? logic_1logic_2logic_3 中的return 0; 语句每次调用后,线程的资源是否会被释放? > 功能?也许我应该使用一个线程数组,并为这个数组的每个项目调用 pthread_join 函数?

最佳答案

您应该在循环内而不是在循环外调用 pthread_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).

关于c - 多次 pthread_create 调用后出现 `Cannot allocate memory` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569631/

相关文章:

java - 谷歌数据流: set a DefaultUncaughtExceptionHandler

C 如何使用 cmd 传递参数并在 execvp 中使用它

c - C 中使用 qsort 对多维数组进行排序

linux - 不允许 Amazon EC2 Apache 符号链接(symbolic link)

Linux >2.6.33 : could sendfile() be used to implement a faster 'cat' ?

java - 无状态对象总是线程安全的?

c - 二维数组的指针数组的最大大小?

c - CUDA的__shared__内存什么时候有用?

c - 在 C 程序中使用 glib 库

java - Android Realm - 从服务访问 Realm 对象