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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:42:54+00:00 2026-06-10T05:42:54+00:00

In a multi-threaded program where 2 threads – thread 1 – will run a

  • 0

In a multi-threaded program where 2 threads –
thread 1 – will run a loop that increases an integer variable 1000 times and then exits
thread 2 – will run a loop that decreases an integer variable 1000 times and then exits

Both threads wait on a semaphore and start roughly at the same time and are scheduled to run on different cores approximately same time.

After both the threads exit, will the value of the integer variable will be zero?
Note: no locking(mutex etc..) is used

Let us assume linux and x86 architecture and multi-core hardware.

What happens for above if the same integer is declared as volatile (C++) ?

  • 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-10T05:42:56+00:00Added an answer on June 10, 2026 at 5:42 am

    If more than one thread modifies the same memory location at the same time the program has a data race and the effects are undefined. The result can be pretty much anything, assuming you get a result at all. For simple variables like integer types, atomics will eliminate the data race and provide proper synchronization. Use atomic_int (also known as atomic<int>).

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

Sidebar

Related Questions

Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
There is a multi threaded batch processing program, that creates multiple worker threads to
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I am on MacOSX. I am writing a multi threaded program. One thread does
My question is how to run a multi-threaded program in C by pthread across
Can gprof be used to profile a multi-threaded program that uses pthreads? That is,
I currently have a multi-threaded program that crawls websites and writes their text to
When I run a single-threaded program that i have written on my quad core
I have a multi-threaded Java application that will output information about a message it
I'am writing a multi-threaded program in C. Before creating the threads, a global python

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.