0

Two threads are executing a function named job, inside which they are incrementing a global variable. Will there be a race condition here?

int i = 0;

void *job(void *args)
{
    i += 1;
}
wowonline
  • 1,061
  • 1
  • 10
  • 17
  • 7
    Unsynchronized access to a non-atomic object in multiple threads, with at least one a write, is always a _data race_ and undefined behavior. – user17732522 Sep 18 '22 at 18:21

1 Answers1

3

Yes, this might result in parallel access to the variable, which is a problem. To avoid that, it's recommended to declare i as std::atomic<int>.

lorro
  • 10,687
  • 23
  • 36