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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:55:40+00:00 2026-05-29T05:55:40+00:00

I’m reading a POSIX threading book for some practice, and I was trying to

  • 0

I’m reading a POSIX threading book for some practice, and I was trying to work out where I’d need mutex guards in a simple singly-linked list as a little practice problem. For example, if I had a list of node structures:

template <typename T>
struct Node
{
    Node<T>* next;
    T data;
};

Node<T>* head = NULL;

//Populate list starting at head...


[HEAD] --> [NEXT] --> [NEXT] --> [NEXT] --> [...] --> [NULL]

and I had two or more threads. Any thread can insert, delete, or read at any point in the list.

It seems if you just try and guard individual list elements (and not the whole list), you can never guarantee another thread isn’t modifying the one the next* pointer points to, so you can’t guarantee safety and maintenance of invariants.

Is there any more efficient way to guard this list than making all operations on it use the same mutex? I would have thought there was but I really can’t think of it.

Also, if it were a doubly linked list does the situation change?

  • 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-29T05:55:41+00:00Added an answer on May 29, 2026 at 5:55 am

    If you want to-do a fine-grained locking approach with a singly linked list (i.e., one lock per node), then you will need to-do the following:

    1. Add two sentinel nodes to the list for the head and tail. Both these nodes have locks associated with them, and every new node will be added between the two of them.
    2. For ever function that traverses the list, you need to obtain a lock on the next node before you assign it to a current pointer. You also cannot release the lock on the current node until you’ve obtained the lock on the next node. If you are also using a prev pointer for traversal, you will keep the lock on that “previous” node until you re-assign the prev pointer to the current pointer.
    3. For adding a node, you will need to lock the two nodes that are going to be on either side of the node you’re adding. So for instance, you would have a prev node pointer, and a current node pointer. You would first lock the mutex on the prev node, and then lock the mutex on the current node, and add the new node in-between the prev and current node.
    4. If you are removing a node, you would again lock the mutex in the prev and current node (in that order) and then you can remove the current node.

    Keep in mind that steps #3 and #4 work because of step #2 where traversing the list requires obtaining locks on the nodes. If you skip that step, you will end up creating dangling pointers and other problems related to mis-assigned pointers as another thread changes the topology of the list underneath the current thread.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.