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;
}
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;
}
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>
.