hash tables, pthread_key_create

I want to store a bunch of pthread_t types in a hash table, but since pthread_t is not an integer value, I cannot hash it. I was hoping to store a unique nonzero as key 0 for each thread with thread-specific data ala pthread_key_create/pthread_setspecific, but but as it turns out only the first thread to create a pthread_key_t can get a key #0! And storing an array to keep track of which thread has which key would defeat the whole point of a hash table.

So is there any way I can store one single integer value per posix thread, anonymously? I can't keep a key index or a pointer because it needs to be accessible from a signal handler.

I'm guessing that your pthread_t is a struct * because the other implementations use long or unsigned long. pthread_t is actually a pointer to the struct in that case.

Unless you're using hcreate(), you can design your own function to compare those structs. Since each one will be unique I do not see a problem. Store your pointer to the struct as a "long".

My pthread_t actually IS an integer, but I also want this to be portable. :slight_smile: I just solved my problem though -- I totally misunderstood pthread keys, I thought there was only one value per key when there's actually one value PER THREAD per key. So I can create one totally global key and store a unique integer for each thread in it :slight_smile: