Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:59:41+00:00 2026-06-06T15:59:41+00:00

I have a number of classes that register with a notification class in my

  • 0

I have a number of classes that register with a notification class in my program. On a particular incoming event, the notifier calls an update() function in each class. In two of these callee’s I’m now trying to spawn a small helper thread which will wait a few seconds, then initialize some external hardware and return.

The helper threads are created with the PTHREAD_CREATE_DETACHED attribute, so I can avoid joining them.

This works fine for one of the threads (spawned from class1), but pthread_create() fails with EAGAIN in the other (from class2). According to the pthread_create() documentation, this happens if the application is out of resources. However, if I provide another incoming event, thread 1 can once again create its helper thread – while thread 2 still fails with the same error – and I can keep doing this with the same result.

The code I’m using is the same in both classes:

class class1_2 {
  public:
    void update();
  private:
    static void *init(void *self);
    pthread_t initThread;
    pthread_attr_t initThreadAttr;
}

void class1_2::update()
{
  printf("Class%d update()\n", classNumber);
  pthread_attr_setdetachstate(&initThreadAttr, PTHREAD_CREATE_DETACHED);
  int retval = pthread_create(&initThread, &initThreadAttr, init, this);
  printf("Class%d thread created = %d\n", classNumber, retval);
}

void *class1_2::init(void *self)
{
  printf("Class%d init()\n", self->classNumber);
  //wait 3 seconds, then do stuff (~1 additional second)
  return;
}

In gdb I get the following output:

<incoming event>

Class1 update()
[New thread 1]
Class1 thread created=0

Class2 update()
Class2 thread created=11 (EAGAIN)

Class1 init()
[Thread 1 exited]

<incoming event>

Class1 update()
[New thread 2]
Class1 thread created=0

Class2 update()
Class2 thread created=11 (EAGAIN)

Class1 init()
[Thread 2 exited]

(...and so it goes on)

I’ve tried commenting out the thread creation in class1 (which works) – but class2 still fails, prompting me to think there is a subtle difference between the classes. The shown code is the same in both classes though, so if anyone has a clue as to what other variables/functions/etc could have an influence, or what else could be going on, I’d be very happy to hear it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:59:42+00:00Added an answer on June 6, 2026 at 3:59 pm

    For rep-whore purposes I’ll promote my comment to an answer in case it’s right 😉

    Ensure you are calling pthread_attr_init to initialize the attribute object before you call pthread_attr_setdetachstate, and don’t forget to destroy the attribute again too:

     void class1_2::update()
    {
      printf("Class%d update()\n", classNumber);
      pthread_attr_init(&initThreadAttr);
      pthread_attr_setdetachstate(&initThreadAttr, PTHREAD_CREATE_DETACHED);
      int retval = pthread_create(&initThread, &initThreadAttr, init, this);
      printf("Class%d thread created = %d\n", classNumber, retval);
      pthread_attr_destroy(&initThreadAttr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a method, Register that subscribes to a number of
I have a number of classes that look like this: class Foo(val:BasicData) extends Bar(val)
I have a number of classes that derive from a pure virtal base: class
I have a number of classes that represents business transaction calls: executing appropriate stored
I have a number of LINQ classes that inherit from the same base class.
I have a number of test classes and methods that copy a particular directory
I have a number of classes that are all related conceptually, but some more-so
I have a number of classes that inherit from one superclass. The superclass as
I have a number of DAO classes that extend SqlMapClientDaoSupport, and call getSqlMapClientTemplate() to
I have a number of static classes that contain tables like this: using System;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.