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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:32:15+00:00 2026-05-18T06:32:15+00:00

I’m currently in the process of developing my own little threading library, mainly for

  • 0

I’m currently in the process of developing my own little threading library, mainly for learning purposes, and am at the part of the message queue which will involve a lot of synchronisation in various places. Previously I’ve mainly used locks, mutexes and condition variables a bit which all are variations of the same theme, a lock for a section that should only be used by one thread at a time.

Are there any different solutions to synchronisation than using locks? I’ve read lock-free synchronization at places, but some consider hiding the locks in containers to be lock-free, which I disagree with. you just don’t explicitly use the locks yourself.

  • 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-18T06:32:15+00:00Added an answer on May 18, 2026 at 6:32 am

    Lock-free algorithms typically involve using compare-and-swap (CAS) or similar CPU instructions that update some value in memory not only atomically, but also conditionally and with an indicator of success. That way you can code something like this:

    1 do
    2 {
    3     current_value = the_varibale
    4     new_value = ...some expression using current_value...
    5 } while(!compare_and_swap(the_variable, current_value, new_value));
    
    • compare_and_swap() atomically checks whether the_variable‘s value is still current_value, and only if that’s so will it update the_variable‘s value to new_value and return true

    • exact calling syntax will vary with the CPU, and may involve assembly language or system/compiler-provided wrapper functions (use the latter if available – there may be other compiler optimisations or issues that their usage restricts to safe behaviours); generally, check your docs

    The significance is that when another thread updates the variable after the read on line 3 but before the CAS on line 5 attempts the update, the compare and swap instruction will fail because the state from which you’re updating is not the one you used to calculate the desired target state. Such do/while loops can be said to “spin” rather than lock, as they go round and round the loop until CAS succeeds.

    Crucially, your existing threading library can be expected to have a two-stage locking approach for mutex, read-write locks etc. involving:

    • First stage: spinning using CAS or similar (i.e. spin on { read the current value, if it’s not set then cas(current = not set, new = set) }) – which means other threads doing a quick update often won’t result in your thread swapping out to wait, and all the relatively time-consuming overheads associated with that.

    • The second stage is only used if some limit of loop iterations or elapsed time is exceeded: it asks the operating system to queue the thread until it knows (or at least suspects) the lock is free to acquire.

    The implication of this is that if you’re using a mutex to protect access to a variable, then you are unlikely to do any better by implementing your own CAS-based “mutex” to protect the same variable.

    Lock free algorithms come into their own when you are working directly on a variable that’s small enough to update directly with the CAS instruction itself. Instead of being…

    • get a mutex (by spinning on CAS, falling back on slower OS queue)
    • update variable
    • release mutex

    …they’re simplified (and made faster) by simply having the spin on CAS do the variable update directly. Of course, you may find the work to calculate new value from old painful to repeat speculatively, but unless there’s a LOT of contention you’re not wasting that effort often.

    This ability to update only a single location in memory has far-reaching implications, and work-arounds can require some creativity. For example, if you had a container using lock-free algorithms, you may decide to calculate a potential change to an element in the container, but can’t sync that with updating a size variable elsewhere in memory. You may need to live without size, or be able to use an approximate size where you do a CAS-spin to increment or decrement the size later, but any given read of size may be slightly wrong. You may need to merge two logically-related data structures – such as a free list and the element-container – to share an index, then bit-pack the core fields for each into the same atomically-sized word at the start of each record. These kinds of data optimisations can be very invasive, and sometimes won’t get you the behavioural characteristics you’d like. Mutexes et al are much easier in this regard, and at least you know you won’t need a rewrite to mutexes if requirements evolve just that step too far. That said, clever use of a lock-free approach really can be adequate for a lot of needs, and yield a very gratifying performance and scalability improvement.

    A core (good) consequence of lock-free algorithms is that one thread can’t be holding the mutex then happen to get swapped out by the scheduler, such that other threads can’t work until it resumes; rather – with CAS – they can spin safely and efficiently without an OS fallback option.

    Things that lock free algorithms can be good for include updating usage/reference counters, modifying pointers to cleanly switch the pointed-to data, free lists, linked lists, marking hash-table buckets used/unused, and load-balancing. Many others of course.

    As you say, simply hiding use of mutexes behind some API is not lock free.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to

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.