Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How bad are data races?
Forum Updated to NodeBB v4.3 + New Features

How bad are data races?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 406 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grand_Titan
    wrote on last edited by
    #1

    Can a data race such as when 1 thread reads an int from memory and another thread writes to the same address at the same time cause any real harm, except the obvious data inconsistency?

    W 1 Reply Last reply
    0
    • G Grand_Titan

      Can a data race such as when 1 thread reads an int from memory and another thread writes to the same address at the same time cause any real harm, except the obvious data inconsistency?

      W Offline
      W Offline
      wrosecrans
      wrote on last edited by
      #2

      @Grand_Titan You basically get to undefined behavior, so theoretically almost anything could happen including a hard crash or silent data corruption.

      In practice, you are likely to get some sort of scrambled nonsense value for the integer which might be fine in some scenarios. If the int is the number of items in an array, you will likely wind up reading/writing past the end of the array and having a buffer overflow issue. If you just display the value of the int on screen, your app may blip and say the temperature outside is negative 3 billion degrees or something. But it's almost impossible to be certain that any given value is never going to be used in a way that could be a problem, so you generally need to fix these sorts of bugs.

      1 Reply Last reply
      3
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Depending on bunch of factors, like the cpu architecture and data alignment, a read/write to an int can be atomic, so sometimes it's ok. But sometimes it's not and that's when fun begins. It's undefined behavior land. Problems range from harmless small inconsistencies to major security vulnerabilities.

        As a simple example imagine writing a size of a container during shrinking operation and reading number of elements at the same time to read from the last one. Again depending on bunch of factors this can lead to reading/writing out of bounds, which, if it happens not to crash your app, is a serious vulnerability. Other example are data races in locking mechanisms that can result in various desynchronization scenarios.

        It all varies, depending on what your int is doing and how far reaching influence it has on the entire system.

        So how bad are data races? As a rule of thumb - Very bad. Fix it.

        1 Reply Last reply
        3
        • G Offline
          G Offline
          Grand_Titan
          wrote on last edited by
          #4

          Thank you for clearing this up for me.

          1 Reply Last reply
          0
          • G Grand_Titan has marked this topic as solved on
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            The x86 architecture has some really weird rules for data races. There are even combinations that seem to be totally illogical at first (check out slide 29 here: https://www.cse.unsw.edu.au/~cs9242/23/lectures/07b-smp.pdf). During optimization compilers (and CPU cores themselves) can do instruction reordering because there are no dependencies between variables inside a single thread.

            Any data race can be really hard to debug (especially because it is usually not deterministic!). So, I agree with @Chris-Kawa: "Very bad. Fix it."

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved