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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:15:34+00:00 2026-06-03T05:15:34+00:00

I have a program with multiple pthreads. These threads report data back to static

  • 0

I have a program with multiple pthreads. These threads report data back to static variables through get and set functions in a common file.

static int LAT;
void getLat(){
    return LAT;
}
void setLAT(int lat){
    LAT = lat;
}

What will happen if one thread is executing getLAT(); and another is executing setLAT(); at the same time? Will my program crash? If so how can I prevent this?

  • 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-03T05:15:35+00:00Added an answer on June 3, 2026 at 5:15 am

    This is called a race condition and results in undefined behavior. Anything could happen. A crash is unlikely, what’s more likely is that the result could be that of before the write, after the write, or a corrupted in-the-middle-of-the-write value.

    If you attempt to use that corrupted value in a system call or as a pointer offset, etc. would be a crash at that point.

    Some platforms and architectures will guarantee atomic reads/writes for objects of a certain size aligned to a certain address (e.g. atomic 4 byte reads if aligned to a 16byte address, etc.), in which case the intermediate value (corrupt) will not happen. But you can’t really rely on that behavior as it isn’t portable. And even when that behavior isn’t guaranteed, your code will likely run fine 99% of the time, except for that one percent in production when you’ll wish you hadn’t taken the shortcut and not bothered with synchronization.

    You need to use a mutex or a atomic read/write instruction like cmpxchg to protect your code from this race condition.

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

Sidebar

Related Questions

I have a program that works with multiple threads. For each I set uncaught
So, I'm implementing a program with multiple threads (pthreads), and I am looking for
I have a program that scales badly to multiple threads, although – theoretically –
I have a program where the user can enter multiple email addresses to get
I have a program that brings up and tears down multiple threads throughout its
I have a ressource manager that handles multiple TCP-Connections. These connections are pthreads. How
I have a C++ program in which I have multiple threads writing into my
I have a C# Winforms program with multiple textboxes. I used the properties for
I have multiple custom NSViews in my Cocoa program. I am looking for a
I have a program where I allocate multiple buffers as a part of a

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.