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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:49:22+00:00 2026-06-11T02:49:22+00:00

Is this code threadsafe? Should I have volatile in the function sig? (ex: void

  • 0

Is this code threadsafe? Should I have volatile in the function sig? (ex: void Unlock() volatile {v=0;}) If it isn’t how do i make this threadsafe?

class SimpleLock {
    std::atomic<int> v;
public:
    bool try_lock() { int z=0; return v.compare_exchange_strong(z, 1); }
    void lock() { while(try_lock()==false) std::this_thread::yield(); }
    void unlock() {v=0;}
};
  • 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-11T02:49:24+00:00Added an answer on June 11, 2026 at 2:49 am

    Yes, it is thread-safe, although you could rename Lock to TryLock since you are not calling CAS in a loop until it succeeds. Traditionally Lock operations are supposed to block until the acquire succeeds.

    Regarding volatile, the docs of std::atomic specify (about the = operator):

    Atomically assigns a value t to the atomic variable. Equivalent to store(desired).

    Then about store:

    void store( T desired, memory_order = std::memory_order_seq_cst );

    Then about memory_order = std::memory_order_seq_cst:

    • No writes in the writer thread can be reordered after the atomic
      store
    • No reads in the reader thread can be reordered before the atomic load.
    • The synchronization is established between all atomic operations tagged std::memory_order_seq_cst. All threads using such atomic
      operation see the same order of memory accesses.

    So no, you don’t need volatile here. Additionally, volatile has weaker guarantees than the ones above (in fact, volatile is mostly useless in C++):

    Within a thread of execution, accesses (reads and writes) to all
    volatile objects are guaranteed to not be reordered relative to each
    other, but this order is not guaranteed to be observed by another
    thread, since volatile access does not establish inter-thread
    synchronization.

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

Sidebar

Related Questions

This code disabled mouse scroll functionality, when i click on the document. $(document).on(click, function
This code produces a different output in Python 2 and Python 3 . class
I have this piece of code in my application. I suspect that it is
Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
I have dll method that should be QoSed - this method should be called
I have this c++ code (VS 2008): LONGLONG res = InterlockedIncrement64(&m_longlong); running along it,
This code below allows me to find the word error in all my files
This code is the core of a much larger script that works great in
This code only renders a dodecahedron and completely ignores the glBegin(GL_TRIANGLES) block: glutSolidDodecahedron(); glBegin(GL_TRIANGLES);
This code gives me this error:Cannot implicitly convert type ArrayList[] to ArrayList[][] at 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.