由于某些原因,pthread_create
不允许我将struct
作为参数传递。这个问题与系统无关,尽管我还没有机会在任何人的机器上测试它。由于某种原因,它不允许我传递struct
;它返回错误#12。
问题不在内存。我知道12是ENOMEM,“应该是这样”,但它不是..它就是不接受我的struct作为指针。
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。
有什么想法吗?
发布于 2011-12-31 12:02:11
Linux上的错误代码12:
#define ENOMEM 12 /* Out of memory */
您的内存可能已用完。确保您没有分配太多的线程,并确保在线程分配完成时使用pthread_join
线程(或使用pthread_detach
)。确保你不会通过其他方式耗尽你的内存。
发布于 2011-12-31 12:02:50
将堆栈对象作为参数传递给pthread_create是一个非常糟糕的想法,我会将其分配到堆上。错误12是ENOMEM。
发布于 2011-12-31 12:03:03
尝试添加一些适当的错误处理。
#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
(内存不足)。
https://stackoverflow.com/questions/8686171
复制相似问题