Saustin
Saustin

Reputation: 1147

Why does pthread_create() return 12?

For some reason, pthread_create isn't allowing me to pass a struct as an argument. 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; it returns error #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.

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.

Any ideas?

Upvotes: 1

Views: 4195

Answers (3)

Dietrich Epp
Dietrich Epp

Reputation: 213368

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).

Upvotes: 1

Ana Betts
Ana Betts

Reputation: 74652

Passing a stack object as a parameter to pthread_create is a pretty bad idea, I'd allocate it on the heap. Error 12 is ENOMEM.

Upvotes: 2

bdonlan
bdonlan

Reputation: 231203

Error code 12 on Linux:

#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). Make sure you're not exhausting your memory through other means as well.

Upvotes: 7

Related Questions