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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:24:30+00:00 2026-06-01T12:24:30+00:00

The code is in C. I have two type of objects ( structs )

  • 0

The code is in C. I have two type of objects (structs) that have parent-child relationship, one parent-type can have 0 or more child-types, a child can’t have its own children. I need O(1) parent lookup (by uID struct member) and child lookup (also by uID struct member) without knowing who’s its parent. Once I’ve got a pointer to a parent, I want to be able to iterate through its children. And when I have a pointer to a child, I want to be able to know who’s its parent. During the execution of the program any child or any parent can be removed or inserted, and a child can change its parent. When parent is removed, its children should be also removed. And all this should be done it multi-threaded environment, so I need thread-safe reads (I will use read-only lock for key searching and read-write lock for insertion/deletion/re-parenting). What data structure would you recommend?

Added:

Currently I’m trying to implement it using uthash library ( http://uthash.sourceforge.net/ ):

struct parent
{
    uint64_t uid;
    time_t mtime;
    struct ldata data;
    struct child *first_child;
    UT_hash_handle hh;
};

struct child
{
    uint64_t uid;
    time_t mtime;
    struct ldata data;
    struct parent *parent;
    UT_hash_handle hh;
};

struct parent *parents_list = NULL;
struct child *children_list = NULL;

The problem is when a new child arrives it ends up being at the tail and
is not connected with its “brothers”.

  • 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-01T12:24:31+00:00Added an answer on June 1, 2026 at 12:24 pm

    How about:

    1. A hash table for parents.
    2. A separate hash table for children.
    3. A link in each child to its parent.
    4. A link in each child to its next and prev siblings (double linked list).
    5. A link in each parent to its first child.

    The hash tables may not be quite O(1) lookup, but they will be close. You can probably use an existing, well-polished library for them.

    In terms of thread safety, you could have mutexes for both hashes (for item insertion/deletion), and also a mutex in each parent, for when it or any of its children are being manipulated. Beware of deadlocks, of course: e.g. if changing a child’s parent requires locking both the old and new parents, make sure you do them in a consistent order!

    Finding lock-free structures would be even better, of course, but I can’t really advise you there, except to research and see if you can find any that seem to fit.

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

Sidebar

Related Questions

Trying to send a complex type between two systems that have the same code
I have two objects of the same type and need to copy property values
For example i have two classes A and B, such that for two objects
I have two objects that contain some properties that are exactly the same (same
I have two objects(of the same type) which contains a prop myprop of type
I have two collections of objects of different type. Lets call them type ALPHA
In one of my projects, I have two data transfer objects RecordType1 and RecordType2
I have two code bases of an application. I need to copy all the
We have two code bases for testing and production. The only differences between these
Hi guys I wrote this code and i have two errors. Invalid rank specifier:

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.