为什么pthread_create()返回12?

时间:2011-12-31 03:58:52

标签: c pthreads

出于某种原因,pthread_create不允许我传递struct作为参数。这个问题与系统无关,虽然我没有机会在其他任何人的盒子上测试它。由于某种原因,它根本不允许我传递struct;它返回错误#12。

问题不在于记忆。我知道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);

当我在GDB中运行此代码时,出于某种原因,它没有返回代码12 ..但是当我从命令行运行它时,它返回12。

有什么想法吗?

3 个答案:

答案 0 :(得分:6)

Linux上的错误代码12:

#define ENOMEM          12      /* Out of memory */

你可能内存不足。确保您没有分配太多线程,并确保pthread_join线程完成后(或使用pthread_detach)。确保你不会通过其他方式耗尽你的记忆。

答案 1 :(得分:2)

将堆栈对象作为参数传递给pthread_create是一个非常糟糕的主意,我会在堆上分配它。错误12是ENOMEM。

答案 2 :(得分:1)

尝试添加一些正确的错误处理。

#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);

在我的系统上,12是ENOMEM(内存不足)。

相关问题