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

The Archive Base Latest Questions

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

A very simple approach. Two threads. volatile __int32 p=0; One thread (A) only use

  • 0

A very simple approach.
Two threads.

volatile __int32 p=0;

One thread (A) only use

while(1){
    ExecuteAVeryCPUIntesiveThing();
    InterlockedExchange(&p, 0);
}

The other thread (B) uses

while(1){
    if(0==InterlockedCompareExchange(&p,0,0))
        InterlockedExchange(&p, 1);
}

If one try to log this under a system under stress. (a lot of memory swap, io, socket, cpu spikes ..)
the values from A don’t get propagated to B.

In A p seems to have the value of 0.
But from B’s perspective p is stuck at 1.
In my world when A sets the value to 0 B should detect and set the value to 0.
And on real hw this works just like that but not when running on esxi.

This seems to work fine under real hardware and under some virtual system but not under vmware.

Have I done a mindslip or …?

OS guest: win2008 server

code compiled with Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64

Host: esxi 4.1

Update:

Response to comments: yes it will bounce between 0 and 1,
but as written, thread B will not bounce, because the value from p never/or rearly changes in B’s perspective, it will bounce 10-20 times and then stop.

I would like to only execute code block in A ( ExecuteAVeryCPUIntesiveThing();) at very precise moments.

The production code is full with more threads and events, mutex and locks but the fact remains, if I strip and only use the above code I can reproduce it if I generate a lot of cpu, mm, io on the guest os.

  • 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-03T00:00:05+00:00Added an answer on June 3, 2026 at 12:00 am

    This code is a threading race waiting to happen. You probably got it from VMWare because you allocated only one processor to the virtual machine.

    What the code is missing is an interlock that ensures that thread B has seen the value of p change. So if thread A acquired a cpu core and keeps running for a while and thread B is blocked, waiting for a quantum to run then thread A can set p to 0 more than once. Thread B will be oblivious of that since it never got a chance to set p back to 1.

    You’ll need to rethink your lock design. The problem isn’t limited to just VMWare, it can go wrong on a regular machine as well, just a lot less likely. It goes wrong once a month, give or take, impossible to debug. This is otherwise a classic producer/consumer scenario, one you address with a thread-safe queue.

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

Sidebar

Related Questions

Ok this is more of general technology/approach question. We have a very simple web
I'm trying to create a very simple database abstraction, one part of it using
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
Very simple question. What does the term 'seeding' mean in general? I'll put the
Very simple Qt GUI application: On the scene I have multiple circles implemented as
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
Very simple example - hoping for a simple solution: char x[7]; if(fgets(x,5,stdin)) printf(y); else
Very simple questions guys, but maybe I'm just forgetting something. In 64bit linux, a
Very simple question, but I want to start using a consistent naming convention for

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.