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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:17:35+00:00 2026-05-28T00:17:35+00:00

I’ve stress tested the code below and it seems to work fine – what

  • 0

I’ve stress tested the code below and it seems to work fine – what are the dangers of not locking in the simple case where there is a single thread writing and a separate thread reading a simple variable?

Have a class that has a public property…

Public Class PropHolder
    Private _myVar As Double
    Public Property myVar() As Double
        Get
            Return _myVar
        End Get
        Set(ByVal value As Double)
            _myVar = value
        End Set
    End Property
End Class

…and another which uses an instance of this class

Public Class Form1 

Public _propHolder As New PropHolder
Delegate Sub dlgPostVar()

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While Not BackgroundWorker1.CancellationPending
            _propHolder.myVar = someValue  'worker thread writing var
        End While
    End Sub

    Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        While Not BackgroundWorker2.CancellationPending
            Me.Invoke(New dlgPostVar(AddressOf postVal)) 'invoke UI thread to write var
        End While
    End Sub

    Private Sub postVal()
        RichTextBox1.AppendText(_propHolder.myVar.ToString & vbCrLf)
    End Sub

End Class

In the above example myVar is continuously changing and being written to a RichTextBox as fast as the UI can keep up. Not all of the values are captured but this is not important – let’s imagine that the RichTextBox only cares about sampling the current value of myVar at its leisure.

Everything I have read leads me to believe that this is not safe to do but I have yet to make it fail, even when having two threads reading and writing (single reader, single writer) this variable incessantly. What can go wrong here, and why?

Edit : To be more specific, this question is more hypothetical than practical. I know that this situation requires a lock “by the book”, the question is exploring the breadth of potential consquences of not locking in this instance, hopefully in the form of a complete collection of all potential modes of failure.

One example thusfar is a read from a variable being written to :

  • Thread 1 -> Reads low word from memory
  • Thread 2 -> CPU moves low word of new value to memory
  • Thread 2 -> CPU moves high word of new value to memory
  • Thread 1 -> Reads high word from memory

Thread 1 Value is now (HighWord[new])(LowWord[old]) ** corrupt

Program failure is limited to damage caused by processing of this corrupt value.
Other consequences of this failure mode? Are there any?

Further examples? Books/references welcome.

Edit #2 : My intention here is to explore, in the .NET framework, the extent and form of unpredictability and errors which are intrinsic to the platform and specifically not related to the consequences of how any specific program might subsequently fail in the face of such an error.

In the example above, I am doing nothing with a value aside from writing it out as text. If I get a bad value, I write a bad value to text – nothing explodes, the program does not crash, empires do not fall, and the orbit of Mercury remains stable.

  • 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-28T00:17:36+00:00Added an answer on May 28, 2026 at 12:17 am

    You do need a lock in this case as variable writes to double are not atomic. Therefore, it is possible for this to break, just extremely unlikely.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker

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.