获取唯一ID的方法(保证线程安全)

int GetUniqueIndex()
{
	static int nUniqueIndex = 0;

	if ( nUniqueIndex >= 4294967291 ) //2的32次方-1
	{
		InterlockedExchange((volatile LONG *)&(nUniqueIndex),1);	
	}
	else
	{
		InterlockedIncrement((volatile LONG *)&nUniqueIndex);
	}

	return nUniqueIndex;
}

猜你喜欢

转载自blog.csdn.net/shaoyiju/article/details/77152207