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

后端 未结 5 1465
[愿得一人]
[愿得一人] 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条回答
  •  抹茶落季
    2020-12-15 18:49

    The real answer is - it depends...

    On most platforms, yes it's safe as long as you don't do anything unsafe in the function which others have mentioned. It's easy to mess up so be careful!

    On other platforms it's most definately unsafe. For example most C compilers for the smaller PIC microcontrollers can't support this due to hardware limitations.

    In general, yes it's safe though.

提交回复
热议问题