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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:20:51+00:00 2026-06-16T18:20:51+00:00

I read about pthread_detach that it releases the resources acquired by thread when it

  • 0

I read about pthread_detach that it releases the resources acquired by thread when it is called so I did a little experiment but even after detaching the thread, it looks like it resources are not released. Here is the code:

#define SIZE 2048
void *func(void *arg);

int main()
{
    void *x;
    int i;

    pthread_t tid;
    pthread_attr_t attr,attr2;


    int fp=open("SharedMemWithMutex.c",O_RDONLY);


    pthread_attr_init(&attr2);

    pthread_create(&tid,&attr2,func,&fp);

    pthread_join(tid,&x); 

    i=*(int *)x;
    fprintf(stderr,"BEFORE DETACH: read bytes are %d\n",i);
    pthread_detach(tid);

    i=*(int *)x;
    fprintf(stderr,"AFTER DETACH: read bytes are %d\n",i);

    return 0;

}

void *func(void *arg)
{
    int fp=*(int *)arg;

    char buf[SIZE];

    int *readbytes=(int *) malloc(sizeof(int));
    *readbytes=read(fp,buf,SIZE);   

    return readbytes;
}
  • 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-16T18:20:52+00:00Added an answer on June 16, 2026 at 6:20 pm

    Here “resources” only includes internal pthreads resources that are needed to join the thread and find out its exit status.

    It does not cover any other resources that you acquire yourself (heap memory, open files, database connections, etc). It may be helpful to realize that such resources don’t generally have an intrinsic notion of belonging to a particular thread, since threads can freely share them.

    There a several other issues with your code:

    • You can either join a thread or detach it; you can’t do both.
    • It is good practice to check return codes from functions; here pthread_detach() is probably returning an error code.
    • It is unclear what you’re hoping to achieve with repeated i=*(int *)x. Even if readbytes did get automatically deallocated, the second i=*(int *)x would simply result in undefined behaviour, which may or may not have manifested itself in any particular way.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read about a string/word that did so the Routing crashed but I can't
I read about the invalidate overload that takes a rectangle, but I didn't understand
I've read about synchronized thread in Posix threads tutorial . They say that function
Read about the z-index issue and tried anything, but just can't seem to fix
I read about __packed__ from here and, I understood that when __packed__ is used
I read about virtual constructors are used for implementing some design patterns, but didn't
I read about 'side effect' from this website : but still not understand why
I read about templated controls/INamingContainer but I can't understand what problem it solves when
I read about JSON from internet but still i have not got the grasp
I read about Drivers that uses JDBC API in a book. Not clear about

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.