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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:06:07+00:00 2026-06-03T06:06:07+00:00

Imagine a structure like this: struct my_struct { uint32_t refs … } for which

  • 0

Imagine a structure like this:

struct my_struct {
    uint32_t refs
    ...
}

for which a pointer is acquired through a lookup table:

struct my_struct** table;

my_struct* my_struct_lookup(const char* name)
{
    my_struct* s = table[hash(name)];

    /* EDIT: Race condition here. */

    atomic_inc(&s->refs);

    return s;
}

A race exists between the dereference and the atomic increment in a multi-threaded model. Given that this is very performance critical code, I was wondering how this race inbetween the dereference and atomic increment is typically resolved or worked around?

EDIT: When acquiring a pointer to a my_struct structure via the lookup table, it is necessary to first dereference the structure in order to increment its reference count. This creates a problem in multi-threaded code when other threads could be altering the reference count and potentially deallocating the object itself while another thread would then dereference a pointer to non-existent memory. Combined with preemption and some bad luck, this could be a recipe for disaster.

  • 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-06-03T06:06:08+00:00Added an answer on June 3, 2026 at 6:06 am

    As someone said above, you can make linked list of memory to free at some later time, so your pointers are never invalid. This is a handy method in some cases.

    Or….you can make a 64 bit struct with your 32 bit pointer and have 32 bits for a ref count and other flags. You can use 64 bit atomic ops on the struct if you wrap it in a union:

    union my_struct_ref {
        struct { 
          unsigned int       cUse     : 16,
                             fDeleted : 1;    // etc 
          struct my_struct  *s;
        } Data;
        unsigned long n64;
    } 
    

    You can human readably work with the Data part of the struct, and you can use CAS on the n64 bit part.

    my_struct* my_struct_lookup(const char* name)
    {
        struct my_struct_ref Old, New;
        int iHash = hash(name);
    
        // concurrency loop
        while (1) {
          Old.n64 = table[iHash].n64;
          if (Old.Data.fDeleted)
            return NULL;
          New.n64 = Old.n64;
          New.Data.cRef++;
          if (CAS(&table[iHash].n64, Old.n64, New.n64)) // CAS = atomic compare and swap
            return New.Data.s; // success
          // we get here if some other thread changed the count or deleted our pointer
          // in between when we got a copy of it int old.  Just loop to try again.
        } 
    }
    

    If you are using 64 bit pointers you will need to do 128 bit CAS.

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

Sidebar

Related Questions

I have a C structure that looks like this typedef struct event_queue{ Event* event;
I have a structure like struct board{ char name; int values[37]; }board Imagine a
Imagine I have a table which stores a series of sparse vectors. A sparse
Imagine I have a directory structure like so: parentDir\dirA\foo\ parentDir\dirB\foo\ parentDir\dirC\ parentDir\dirD\bar\foo\ parentDir\dirE\foo\ parentDir\dirF\
In our code we used to have something like this: *(controller->bigstruct) = ( struct
Imagine a table with the following structure on PostgreSQL 9.0: create table raw_fact_table (text
Imagine this structure: /project /templates view1.haml view2.haml misc_views/ view3.haml view4.haml even_deeper/ view5.haml /public script1.js
imagine this layout of classes i use for building a tree structure: class Treenodebase
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
Imagine the following folder structure: project src code.c makefile bin How can I compile

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.