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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:59:47+00:00 2026-05-11T18:59:47+00:00

I’m using a map as a thread specific cache to keep track of failed

  • 0

I’m using a map as a thread specific cache to keep track of failed LDAP searches. I dynamically allocate the map and store the pointer using pthread_setspecific. When checking the cache or incrementing the failure count I use pthred_getspecific in order to retrieve the void* pointer and static_cast the pointer back to my map type. Calls to the map using the [] operator don’t appear to affect the state of the map and calls to map->size() always return 0. It feels like I’m probably incorrectly using pthread_getspecific but from the examples I’ve looked at my code looks right.

Code:

typedef std::map<std::string, int>  FailedSearchCacheMap;

/**
 * Create the keyserver failed search cache key. Only called 
 * internally and may only be called once per thread.
 */
static void
sCreateKeyserverFailedSearchCache(void)
{
    // Create the key used in refrencing the cache.  
    // sFreeKeyserverFailedSearch called against the pointer when the thread exits
    pthread_key_create(&sFailedSearchCacheKey, sFreeFailedSearchCache);
}

/**
 * Get the keyserver failed search cache (also create one if it doesn't exist)
 */
static FailedSearchCacheMap *
sGetKeyserverFailedSearch(void)
{
    // Initializes the failed search cache key.  
    // pthread_once guarantees that the create key routine is only called once
    static pthread_once_t sKeyserverFailedSearchOnce = PTHREAD_ONCE_INIT;
    pthread_once(&sKeyserverFailedSearchOnce, sCreateKeyserverFailedSearchCache);

    FailedSearchCacheMap* cache = static_cast<FailedSearchCacheMap *>(pthread_getspecific(sFailedSearchCacheKey));
    if (IsNull(cache))
    {
        cache = new FailedSearchCacheMap();
        pthread_setspecific(sFailedSearchCacheKey, cache);
    }

    return cache;
}

Test Code:

FailedSearchCacheMap* map_ptr1 = sGetKeyserverFailedSearch();
FailedSearchCacheMap* map_ptr2 = sGetKeyserverFailedSearch();

std::string ks("hostname");
FailedSearchCacheMap map1 = *map_ptr1;
FailedSearchCacheMap map2 = *map_ptr2;

int numFailedSearches = map1[ks] + 1;
map1[ks] = numFailedSearches;

std::cout << "numFailedSearches: "  << numFailedSearches << std::endl;

std::cout << "map_ptr1 address: "   << map_ptr1 << std::endl;
std::cout << "map_ptr2 address: "   << map_ptr2 << std::endl;

std::cout << "map_ptr1->size(): "   << map_ptr1->size() << std::endl;
std::cout << "map_ptr2->size(): "   << map_ptr2->size() << std::endl;

std::cout << "map1.size(): "        << map1.size() << std::endl;
std::cout << "map2.size(): "        << map2.size() << std::endl;

FailedSearchCacheMap::iterator i = map1.begin();
for(; i != map1.end(); i++)
    std::cout << (*i).first << ":" << (*i).second << std::endl;

Test Code Output:

numFailedSearches: 1
map_ptr1 address: 0x909ce88
map_ptr2 address: 0x909ce88
map_ptr1->size(): 0
map_ptr2->size(): 0
map1.size(): 1
map2.size(): 0
hostname:1
  • 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-11T18:59:47+00:00Added an answer on May 11, 2026 at 6:59 pm

    When your test code calls sGetKeyserverFailedSearch(), it is then assigning the pointers to local map variables, thus making copies of the map contents. Any changes you make to those variables will not be reflected in the original maps that you store with pthread_setspecific(), as evident by your logging (map1’s size incremented but map_ptr1’s size did not). Any modifications you want to make to the original maps must be done using the pointers that sGetKeyserverFailedSearch() returns, for example:

    FailedSearchCacheMap* map_ptr = sGetKeyserverFailedSearch();
    std::string ks("hostname");
    
    int numFailedSearches = (*map_ptr)[ks] + 1;
    (*map_ptr)[ks] = numFailedSearches;
    
    std::cout << "numFailedSearches: " << numFailedSearches << std::endl;
    std::cout << "map_ptr address: " << map_ptr << std::endl;
    std::cout << "map_ptr->size(): " << map_ptr->size() << std::endl;
    
    FailedSearchCacheMap::iterator i = map_ptr->begin();
    for(; i != map_ptr->end(); i++)
        std::cout << (*i).first << ":" << (*i).second << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • 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 Create a redirection handler. Link May 12, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer Floating the header left will close the gap in ie6/7.… May 12, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer require 'rubygems' require 'net/ssh/multi' ...should take care of you. May 12, 2026 at 11:37 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.