Question

An existing code of mine makes use of some thread specific keys. I intend to scale this code now which requires creating more and more such keys. But pthread supports around 1024 keys per process due to which keys are not created after this limit which creates a problem for me. Any suggestions on how best same functionality could be achieved without using the pthread implementation of keys?

Was it helpful?

Solution

Use Thread-Local storage:

#define KEYS_MAXIMUM 2048

__thread void * keys[KEYS_MAXIMUM];

To allow destructors you might use:

typedef struct 
{
  void * p;
  void (*f) (void *);
} Key

__thread Key * keys[KEYS_MAXIMUM];

All that is missing here is some wrappers mimicing the pthread_key* API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top