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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:03:20+00:00 2026-05-26T00:03:20+00:00

Simple edition: In a C++ program I’m using two different threads to work with

  • 0

Simple edition: In a C++ program I’m using two different threads to work with some integer variable. but I’m sure one is always writing some value into it and the other one is only reading That. do I still need to use mutex lock when reading/writing data?

Now details : The main idea is that first thread generates some information and saves them into an array, and the second thread reads data from that array and process them. this array represents a queue. meaning I have two index values pointing to the first and last item in queue. Now I’m wondering if I have to lock these two index values whenever I’m reading or writing values or is it ok to check them without locking? note that generator thread is the only thread changing index of queue_back, and processor thread has exclusive permission to change queue_front.

if makes any change I’m developing for a linux based system and the code is compiled using gcc.

PS: In some codes which use threading, I’ve seen keyword volatile around variables shared between different threads, do I need to use that too?

  • 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-26T00:03:21+00:00Added an answer on May 26, 2026 at 12:03 am

    No the read and write are not atomic, You will need to synchronize it using some synchronization mechanism.

    Also, You MUST mark the shared integer as volatile, otherwise the optimizer might think the variable is never updated in one of your threads.

    gcc allows you to do atomic operations on int‘s, long‘s and long long‘s (and their unsigned counterparts).

    Look up for the functions:

    type __sync_fetch_and_add (type *ptr, type value);
    type __sync_fetch_and_sub (type *ptr, type value);
    type __sync_fetch_and_or (type *ptr, type value);
    type __sync_fetch_and_and (type *ptr, type value);
    type __sync_fetch_and_xor (type *ptr, type value);
    type __sync_fetch_and_nand (type *ptr, type value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question, but one that I've been curious about...is there a functional difference between
Simple yes or no question, and I'm 90% sure that it is no... but
I am writing a simple C++ database, using Visual Studio 2008 express edition, like
I am creating a simple C++ DLL project using Visual Studio 2008 Express Edition.
I'm writing a program using the Winsock API because a friend wanted a simple
I am using red hat enterprise edition n try making a simple php page..
I'm running Windows 7 - x64 Edition with IIS 7.5 I have a simple
Simple ASP.NET application. I have two drop-down controls. On the first-drop down I have
I have developed a simple DB-editing app using Xceed's excellent DataGrid for WPF (UX
In my program I need to open a file using an external application (Bentley

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.