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 7659637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:21:31+00:00 2026-05-31T13:21:31+00:00

My pthread_detach calls fail with a Bad file descriptor error. The calls are in

  • 0

My pthread_detach calls fail with a “Bad file descriptor” error. The calls are in the destructor for my class and look like this –

if(pthread_detach(get_sensors) != 0)
    printf("\ndetach on get_sensors failed with error %m", errno);

if(pthread_detach(get_real_velocity) != 0) 
    printf("\ndetach on get_real_velocity failed with error %m", errno);

I have only ever dealt with this error when using sockets. What could be causing this to happen in a pthread_detach call that I should look for? Or is it likely something in the thread callback that could be causing it? Just in case, the callbacks look like this –

void* Robot::get_real_velocity_thread(void* threadid) {
    Robot* r = (Robot*)threadid;
    r->get_real_velocity_thread_i();
}

inline void Robot::get_real_velocity_thread_i() {
    while(1) {
        usleep(14500);

        sensor_packet temp = get_sensor_value(REQUESTED_VELOCITY);

        real_velocity = temp.values[0];
        if(temp.values[1] != -1)
            real_velocity += temp.values[1];
    }   //end while
}



/*Callback for get sensors thread*/
void* Robot::get_sensors_thread(void* threadid) {
    Robot* r = (Robot*)threadid;
    r->get_sensors_thread_i();
}   //END GETSENSORS_THREAD


inline void Robot::get_sensors_thread_i() {
    while(1) {

    usleep(14500);

    if(sensorsstreaming) {

        unsigned char receive;
        int read = 0;

        read = connection.PollComport(port, &receive, sizeof(unsigned char));

        if((int)receive == 19) {

            read = connection.PollComport(port, &receive, sizeof(unsigned char));

            unsigned char rest[54];

            read = connection.PollComport(port, rest, 54);

            /* ***SET SENSOR VALUES*** */
            //bump + wheel drop
            sensor_values[0] = (int)rest[1];
            sensor_values[1] = -1;
            //wall
            sensor_values[2] = (int)rest[2];
            sensor_values[3] = -1;
            ...
            ...
            lots more setting just like the two above
            }   //end if header == 19
        }   //end if sensors streaming
    }   //end while
}   //END GET_SENSORS_THREAD_I

Thank you for any help.

  • 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-05-31T13:21:33+00:00Added an answer on May 31, 2026 at 1:21 pm

    The pthread_* functions return an error code; they do not set errno. (Well, they may of course, but not in any way that is documented.)

    Your code should print the value returned by pthread_detach and print that.

    Single Unix Spec documents two return values for this function: ESRCH (no thread by that ID was found) and EINVAL (the thread is not joinable).

    Detaching threads in the destructor of an object seems silly. Firstly, if they are going to be detached eventually, why not just create them that way?

    If there is any risk that the threads can use the object that is being destroyed, they need to be stopped, not detached. I.e. you somehow indicate to the threads that they should shut down, and then wait for them to reach some safe place after which they will not touch the object any more. pthread_join is useful for this.

    Also, it is a little late to be doing that from the destructor. A destructor should only be run when the thread executing it is the only thread with a reference to that object. If threads are still using the object, then you’re destroying it from under them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t
I have a simple server that looks something like this: void *run_thread(void *arg) {
New to pthread programming, and stuck on this error when working on a C++&C
pthread_create(&thread, NULL, AcceptLoop, (void *)this); I have declared like this and inside of the
Is there smth like pthread_barrier in SMP Linux kernel? When kernel works simultaneously on
I know pthread_cond_wait unlocks the mutex attached with it. What i would like to
I am using pthread.h in a *.cc file. when I try to use pthread_exit(0);
I have what at the moment seems like an unsolvable EXC_BAD_ACCESS problem. I've tried
I received that error: pthread_mutex_lock.c:62: __pthread_mutex_lock: Assertion `mutex->_ data. _owner == 0' failed. And
From pthread_key_create manpage : An optional destructor function may be associated with each key

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.