Is it safe for multiple threads to call the same function?

后端 未结 5 1463
[愿得一人]
[愿得一人] 2020-12-15 18:00

Is it safe to for example do:

void AddTwo(int &num)
{
  num +=2;
}


void ThreadProc(lpvoid arg)
{
AddTwo((int)arg);

}

Would it be saf

5条回答
  •  -上瘾入骨i
    2020-12-15 18:45

    As a general rule of thumb, a function is re-entrant if it doesn't alter any common resources (e.g. same memory locations). If it does, you need to use some sort of synchronization mechanism like mutexes or semaphores.

提交回复
热议问题