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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:19:01+00:00 2026-05-30T23:19:01+00:00

In my application, I have an int and a bool variable, which are accessed

  • 0

In my application, I have an int and a bool variable, which are accessed (multiple write/read) by multiple threads. Currently, I am using two mutexes, one for int and one for bool to protect those variables.

I heard about using atomic variables and operators to write lock-free multi-thread program. My questions are

  1. What’s the definition of atomic variables and operators?
  2. What’s the main difference between std::atomic and
    boost/atomic.hpp? Which one is more standard or popular?
  3. Are these libraries platform-dependent? I am using gnu gcc 4.6 on
    Linux at the moment, but ideally it shall be cross-platform. I heard that the definition of “atomic” actually depends on the hardware as well. Can anyone explain that as well?
  4. What’s the best way to share a bool variable among multiple threads? I would prefer not to use the “volatile” keyword.

Are these code thread-safe?

double double_m; // double_m is only accessed by current thread.
std::atomic<bool> atomic_bool_x;
atomic_bool_x = true && (double_m > 12.5);

int int_n; // int_n is only accessed by current thread.
std::atomic<int> atomic_int_x;
std::atomic<int> atomic_int_y;
atomic_int_y = atomic_int_x * int_n;
  • 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-30T23:19:03+00:00Added an answer on May 30, 2026 at 11:19 pm

    I’m not an expert or anything, but here’s what I know:

    1. std::atomic simply says that calling load and store (and a few other operations) concurrently is well-defined. An atomic operation is indivisible – nothing can happen ‘in-between’.
    2. I assume std::atomic is based off of boost::atomic. If you can, use std, otherwise use boost.
    3. They are both portable, with the std being completely so, however your compiler will need to support C++11
    4. Likely std::atomic_bool. You should not need to use volatile.

    Also, I believe load/store differs from operator=/operator T only load/store are atomic.

    Nevermind. I checked the standard and it appears that the operators are defined in terms of load/store/etc, however they may return different things.

    Further reading:

    • http://en.cppreference.com/w/cpp/atomic/atomic
    • C++11 Standard
    • C++ Concurrency in Action
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code similar to this in my application: class A { public: int
In my console application have an abstract Factory class Listener which contains code for
I have application which needs to use a dll (also written by me) which
I have an entity with three fields Task { Active BOOL Recurrent Int Started
I have an application on Vista which loops through all processes and finds associated
I have an application that contains methods that work with data using Entity Framework
I have an application where a multiple shops will share the data. There is
I have an application that logs messages to the screen using a TextBox. The
I have an application which communicates with a database. Each table has a unique
Currently i have an application that reads and writes several properties from one or

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.