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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:07:06+00:00 2026-06-03T06:07:06+00:00

Having mutex locks for critical section says that more than one thread won’t be

  • 0

Having mutex locks for critical section says that more than one thread won’t be able to ruin the critical section, but then why would we need the volatile variable for the same thing?

What would happen if there is no volatile word in the following code?

enter image description here

  • 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-03T06:07:08+00:00Added an answer on June 3, 2026 at 6:07 am

    The volatile keyword makes the double checked locking work correctly on 1.5 and later JVMs, see the Under the new Java Memory Model section of that page.

    If there was no volatile keyword there is a possibility of two instances being created on 1.5 and later JVMs. On pre-1.5 JVMs double checked locking is considered completely broken, and should not be used.

    However, in the case of initialising a singleton, the use of double checked locking is usually considered not worth it and a simple static initialisation will suffice. If you absolutely know the singleton will be initialised at some point by your application, then the lazy instantiation is redundant. Just go for:

    private static Singleton s_instance = new Singleton() 
    

    or whatever the C++ equivalent is. Nice and safe!

    As this question has ended up a rather tangled mess of Java & C++ please note, I am referring to Java above. In C++ there is no synchronized keyword, and volatile is not applicable to concurrency.

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

Sidebar

Related Questions

Singularity - If a thread managed to lock a mutex, it is assured that
For example having an array or reader threads and one writer thread we can
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I am having a problem with mutexes (pthread_mutex on Linux) where if a thread
I'm having some trouble with a program using pthreads, where occassional crashes occur, that
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
Windows CRITICAL_SECTION is implemented in a way lighter than mutex ( as far as
I have 2 threads and a shared float global. One thread only writes to
Is it possible to wait on a boost::condition_variable without having to acquire a mutex
I would like to limit an application to having only one instance running on

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.