简体   繁体   English

为什么pthread_create()返回12?

[英]Why does pthread_create() return 12?

For some reason, pthread_create isn't allowing me to pass a struct as an argument. 由于某种原因, pthread_create不允许我将struct作为参数传递。 The issue is not system related, although I have not had a chance to test it on anyone else's box. 这个问题与系统无关,尽管我没有机会在其他人的盒子上进行测试。 It simply won't allow me to pass a struct for some reason; 出于某种原因,它根本不允许我传递struct it returns error #12. 它返回错误#12。

The issue is not with memory. 问题不在于内存。 I know 12 is ENOMEM, and "that should be that", but it's not.. it simply won't accept my struct as a pointer. 我知道12是ENOMEM,“应该就是那个”,但事实并非如此。它根本不会接受我的结构作为指针。

struct mystruct info;    
info.website = website;
info.file = file;
info.type = type;
info.timez = timez;
for(threadid = 0; threadid < thread_c; threadid++)
   {
    // printf("Creating #%ld..\n", threadid);
    retcode = pthread_create(&threads[threadid], NULL, getstuff, (void *) &info);
   //void * getstuff(void *threadid);

When I ran this code in GDB, for some reason, it didn't return code 12.. but when I run it from the command line, it returns 12. 当我出于某种原因在GDB中运行此代码时,它没有返回代码12。但是从命令行运行时,它返回了12。

Any ideas? 有任何想法吗?

Error code 12 on Linux: Linux上的错误代码12:

#define ENOMEM          12      /* Out of memory */

You are likely running out of memory. 您可能内存不足。 Make sure you're not allocating too many threads, and be sure to pthread_join threads when they're done (or use pthread_detach ). 确保没有分配太多线程,并确保在完成线程后加入pthread_join线程(或使用pthread_detach )。 Make sure you're not exhausting your memory through other means as well. 确保您也不会通过其他方式耗尽内存。

Passing a stack object as a parameter to pthread_create is a pretty bad idea, I'd allocate it on the heap. 将堆栈对象作为参数传递给pthread_create是一个非常糟糕的主意,我将其分配到堆上。 Error 12 is ENOMEM. 错误12是ENOMEM。

Try adding some proper error handling. 尝试添加一些适当的错误处理。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static void fail(const char *what, int code)
{
    fprintf(stderr, "%s: %s\n", what, strerror(code));
    abort();
}

...
if (retcode)
    fail("pthread_create", retcode);

On my system, 12 is ENOMEM (out of memory). 在我的系统上,12是ENOMEM (内存不足)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM