场景一:多线程访问共享资源

多线程场景下,对共享资源访问时如果返回共享资源的指针或引用,那么请使用const修饰,使之成为指向常量的指针。

//如果不加const,那么多线程场景下加锁将毫无意义,因为外部可以随意通过指针和引用随意修改共享资源
const int *func()
{
int* pi = new int;
*pi = 10;
return (const int *)pi;
}

int main()
{
const int *pl;
pl = func();
std::cout << "Hello World!\n";
while (1) Sleep(1000);
}