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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:21:20+00:00 2026-05-31T05:21:20+00:00

I keep hearing about thread safe. What is that exactly and how and where

  • 0

I keep hearing about thread safe. What is that exactly and how and where can I learn to program thread safe code?

Also, assume I have 2 threads, one that writes to a structure and another one that reads from it. Is that dangerous in any way? Is there anything I should look for? I don’t think it is a problem. Both threads will not (well can’t ) be accessing the struct at the exact same time..

Also, can someone please tell me how in this example : https://stackoverflow.com/a/5125493/1248779 we are doing a better job in concurrency issues. I don’t get it.

  • 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-31T05:21:21+00:00Added an answer on May 31, 2026 at 5:21 am

    Thread-safety is one aspect of a larger set of issues under the general heading of “Concurrent Programming”. I’d suggest reading around that subject.

    Your assumption that two threads cannot access the struct at the same time is not good. First: today we have multi-core machines, so two threads can be running at exactly the same time. Second: even on a single core machine the slices of time given to any other thread are unpredicatable. You have to anticipate that ant any arbitrary time the “other” thread might be processing. See my “window of opportunity” example below.

    The concept of thread-safety is exactly to answer the question “is this dangerous in any way”. The key question is whether it’s possible for code running in one thread to get an inconsistent view of some data, that inconsistency happening because while it was running another thread was in the middle of changing data.

    In your example, one thread is reading a structure and at the same time another is writing. Suppose that there are two related fields:

      { foreground: red; background: black }
    

    and the writer is in the process of changing those

       foreground = black;
                <=== window of opportunity
       background = red;
    

    If the reader reads the values at just that window of opportunity then it sees a “nonsense” combination

      { foreground: black; background: black }
    

    This essence of this pattern is that for a brief time, while we are making a change, the system becomes inconsistent and readers should not use the values. As soon as we finish our changes it becomes safe to read again.

    Hence we use the CriticalSection APIs mentioned by Stefan to prevent a thread seeing an inconsistent state.

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

Sidebar

Related Questions

Since I have started using this site, I keep hearing about the Boost library.
What is some good software that is written with WPF? I keep hearing about
I keep on hearing about concurrent programing every where. Can you guys throw some
I keep hearing from associates about grid computing which, from what I can gather,
I keep hearing a lot about functors in C++. Can someone give me an
What is this SWX data format that I keep hearing about? The Wikipedia article
I keep hearing that global variables should never be used, but I have a
I keep hearing about DLL hell - what is this all about?
Erm - what the question said. It's something I keep hearing about, but I've
I keep hearing that div tags should be used for layout purposes and not

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.