2011-12-31 102 views
0

出于某种原因,pthread_create不允许我将struct作为参数。这个问题与系统无关,尽管我没有机会在其他人的盒子上测试它。由于某种原因,我根本不允许我通过struct;它返回错误#12。为什么pthread_create()返回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

任何想法?在Linux上

+0

线程例程'getstuff()'的参数是'&info'的指针,它不是一个线程ID,尽管你的注释是它的参数是'threadid'。你可能知道这一点,但只是双重检查。 – 2011-12-31 04:16:51

+0

假设你传递一个空指针而不是'info'的地址;你可以把'info'变成一个全局变量,因为线程都使用相同的信息 - 只是不要在任何线程中修改它。你还会遇到内存问题吗?你有没有将任何文件或共享内存段映射到任何地方?你正在使用32位还是64位平台(编译)?你有没有尝试为'pthread_create()'的第二个参数创建一些线程属性? – 2011-12-31 04:21:12

回答

6

错误代码12:

#define ENOMEM   12  /* Out of memory */ 

你很可能运行内存。确保你没有分配太多的线程,并且一定要在线程完成时(或使用pthread_detachpthread_join线程。确保你没有通过其他方式耗尽你的记忆。

+0

未释放malloc()..谢谢,我没有调试我的线程。 – Saustin 2011-12-31 04:18:54

2

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

+0

我试着用malloc分配它,但它仍然返回12;在对指针使用正确的修改之后,它仍然具有相同的效果..它像它只接受类型为long的ptrs – Saustin 2011-12-31 04:06:41

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(内存不足)。