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

  • Home
  • SEARCH
  • 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 144719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:17:52+00:00 2026-05-11T08:17:52+00:00

From pthread_key_create manpage : An optional destructor function may be associated with each key

  • 0

From pthread_key_create manpage :

An optional destructor function may be associated with each key value. At thread exit, if a key value has a non-NULL destructor pointer, and the thread has a non-NULL value associated with the key, the function pointed to is called with the current associated value as its sole argument. The order of destructor calls is unspecified if more than one destructor exists for a thread when it exits.

If, after all the destructors have been called for all non-NULL values with associated destructors, there are still some non-NULL values with associated destructors, then the process is repeated. If, after at least [PTHREAD_DESTRUCTOR_ITERATIONS] iterations of destructor calls for out- standing non-NULL values, there are still some non-NULL values with asso- ciated destructors, the implementation stops calling destructors.

I’ve wrote a little example with a simple destructor printing ‘Hello World’ for a non NULL thread specific value. As far as I can see, this destructor is called only once (at least on linux fedora and mac os x) even if the thread specific value is still not NULL after the first call to destructor.

Have I missed something?! (PTHREAD_DESTRUCTOR_ITERATIONS = 4 on glibc.)

Here is my little example :

#include <pthread.h> #include <stdio.h> #include <stdlib.h>  #define NB_THREADS 1 #define NB_KEYS 1  static pthread_key_t keys[NB_KEYS]; static pthread_mutex_t mutex;  void destruction (void *arg) {   (int) arg ++;   printf ('Destructor called! -- key value : %i\n', (int)arg); }  void* startup_routine(void* argv) {   int i;   int th = (int) argv;    for (i = 0; i < NB_KEYS; i++)     pthread_setspecific(keys[i], (void*) ((th + i)* 2));    pthread_mutex_lock(&mutex);    printf('Thread %i\n', th);    for (i = 0; i < NB_KEYS; i++)     printf ('\tkeys[%i] : %i\n', i, (int)pthread_getspecific(keys[i]));    pthread_mutex_unlock(&mutex);    return 'End'; }  int main(int argc, char** argv) {   int i;   void *result;   pthread_t thread[NB_THREADS];    for (i = 0; i < NB_KEYS; i++)     pthread_key_create(&keys[i], destruction);    pthread_mutex_init(&mutex, NULL);    for (i = 0; i < NB_THREADS; i++)     pthread_create( &thread[i], NULL, startup_routine, (void*)(i+1) );    for (i = 0; i < NB_THREADS; i++)   {     pthread_join( thread[i], &result );     printf('Return from the thread %i = '%s'\n', i, (char*)result );   }    return 0; } 
  • 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. 2026-05-11T08:17:53+00:00Added an answer on May 11, 2026 at 8:17 am

    Seems there isn’t a lot of people using pthread in here!

    So, again, I’ll answer my own question :

    The destructor will be called more than one time ONLY if a call to pthread_setspecific is done in the destructor, changing the value of the key.

    This is because before calling the destructor, the key pointer is set to null and the pointer is passed to the destructor. So if we want the key pointer not to be null, just have to recall pthread_setspecific in it.

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

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I took Microsoft's event aggregator and wrote it in Java.… May 12, 2026 at 9:12 am
  • Editorial Team
    Editorial Team added an answer How about adding the onclick client event to the button:… May 12, 2026 at 9:12 am
  • Editorial Team
    Editorial Team added an answer Namespaces work on a per-file basis. If myfile.php doesn't declare… May 12, 2026 at 9:12 am

Related Questions

I'm using a map as a thread specific cache to keep track of failed
From pthread_join() man page : When a joinable thread terminates, its memory resources (thread
What does it mean when it gives a backtrace with the following output? #0
I have a pthread_t, and I'd like to change its CPU affinity. The problem

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.