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

后端 未结 5 1468
[愿得一人]
[愿得一人] 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条回答
  •  Happy的楠姐
    2020-12-15 18:58

    There is nothing wrong in calling same function from different threads. If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes, racearound conditions.

提交回复
热议问题