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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:16:47+00:00 2026-05-20T05:16:47+00:00

Why is the following snippet for deleting a node in a linked list not

  • 0

Why is the following snippet for deleting a node in a linked list not thread safe?

edit: note every node has a lock of its own

// ... lock acquisition here
// ... assumption found to be valid here
prev->next = p->next;
p->next = NULL;
p->deleted = 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-20T05:16:48+00:00Added an answer on May 20, 2026 at 5:16 am

    It is thread safe assuming the scope of your lock (meaning what it locks, nothing to do with the official term “scope” used in C) is large enough.

    If it locks just the current node p, then you can’t rely on other threads not coming in and playing with prev (or head or tail for that matter) and hence under-cutting you.

    If it locks the entire structure, then yes, it is thread-safe.

    We can’t tell the scope of your lock from the code given but I will mention one other (unrelated) thing.

    You should probably either free p or add it to a free list for re-use. Simply setting its next pointer to null and its deleted flag to 1 won’t let you find it when you need to reuse it. That will lead to a memory leak. It may be that the code to do this just isn’t shown but I thought I’d mention it, just in case.


    Based on your edit where you state you’re using a fine-grained approach (one lock per node):

    Provided you lock all three of the “nodes” that you’re using or changing, and that you lock them in a consistent direction, it’s still thread-safe.

    I put “nodes” in quotes since it also applies to the head and tail pointers. For example, if you want to delete the first node in a ten-node list, you need to lock the head variable and the first and second nodes, in that order. To delete the last node in a one-node list, you need to lock both the head and tail variables and the node.

    Locking of all three “nodes” will prevent threads from adversely affecting each other.

    Locking them in a consistent direction (such as from head towards tail) will prevent deadlocks.

    But you have to lock all three before attempting to change anything.

    This will even prevent it from concurrent insert operations provided the insert locks the two “nodes” on either side of the insertion point and, of course, locks them in the same direction.

    Not sure how well iterating over the list would go though. You could probably get away with a system whereby you initially lock the head variable and the first node, then release head.

    Then, when you’ve finished with that node, lock the next one before releasing the current one. That way, you should be able to iterate through the list without being affected by inserts or deletes, which can only happen in areas you’re not currently working with.


    But, the bottom line is that you can certainly make it thread safe, even with a fine-grained locking scope.

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

Sidebar

Related Questions

Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
The following snippet, from another thread, works to print a message and fail after
The following snippet of code has been working fine for weeks but today I
Found the following snippet on the Closure page on wikipedia //# Return a list
Examine the following snippet: assertThat( Arrays.asList(1x, 2x, 3x, 4z), not(hasItem(not(endsWith(x)))) ); This asserts that
The following snippet gives output as Sup.field=0, Sup.getField()=1 I do not understand why Sup.getField()
The following snippet is not working for me. Basically I want to check if
The following snippet has worked for validating user entered html snippets for ages, now
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
The following snippet is supposed to take the value of PROJECT (defined in the

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.