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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:34:14+00:00 2026-05-23T03:34:14+00:00

I was wondering about the use of pthread_key_create while passing in a destructor function.

  • 0

I was wondering about the use of pthread_key_create while passing in a destructor function.

I wanted to have something like this:

static ComplexObject foo;

void workoncomplex(void *) {
    foo.dosomestuff();
}

static pthread_key_t pthreadkey;

void function_called_by_threads() {
    pthread_key_create(&pthreadkey, workoncomplex)
}

Obviously I’ve left out a fair amount of detail.

For any thread that isn’t the main thread, this is obviously fine (provided locking etc.), and whenever a thread dies, my workoncomplex function gets called and manipulates the foo object.

My question is, is this valid for the main thread, as obviously the pthreadkey‘s calling of the destructor happens at thread wind down, but is it guaranteed to run before the statics are destructed? If so, would I have to check if I’m in the main thread and just return immediately? Or can I just treat all threads the same and assume my static objects will still be around.

  • 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-23T03:34:15+00:00Added an answer on May 23, 2026 at 3:34 am

    The destructor function is not called on application exit. It is only called when a thread exits.

    If you exit the main thread with pthread_exit() then the destructor function will be called, but the application is not yet being shut down so it is safe to use the static variables.

    If you call exit() or return from main() then the destructor function will not be called, so the fact that the static variables are destroyed is not a problem. Use atexit() to ensure that your destructor function is called on return from main() or on a call of exit().

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

Sidebar

Related Questions

I was wondering what language to use when talking about a function that takes
I've been wondering about this for a while after reading through an interesting article
This may seem like a daft question, but i was wondering about how to
I'm wondering about computational efficiency. I'm going to use Java in this example, but
I was wondering about how to write do-while-style loop? I found this post :
I have been wondering about this, which is why I have put off learning
I've been wondering about this for a while and I can't see why Google
I have been wondering about a function in Java, which I hope exists. I
Just wondering about this, is it possible to use Django with the Google Apps
I was wondering about the logic in this function declaration: CMyException (const std::string &

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.