生成唯一流水号
2016-04-22 10:52 sylar_liang 阅读(333) 评论(0) 编辑 收藏 举报1.定义枚举
//序列号类型
enum SEQUENCE_NUMBER
{
SN_X = 1,
SN_Y = 2,
SN_Z = 3
};
2.定义 流水号结构
struct _GlideNumber
{
_GlideNumber(){} //初始化
_GlideNumber(const _GlideNumber &in){} //拷贝构造函数
_GlideNumber& operator =(const _GlideNumber &in){} //赋值构造函数
//变量
long lCount; //当前是第几个
long lNumber; //序列号
Mutex *_mutux; //锁
};
3.产生序列号
long CreateGlideNum(int nType)
{
static map<int, _GlideNumber> mapGlideNum;
static Mutex _mtx;
_mtx.lock();
map<int, _GlidNumber>::iterator it;
it = mapGlideNum.find(nType);
if (it == mapGlideNum.end())
{
_GlideNumber * lpGN = new _GlideNumber;
mapGlideNum.insert(make_pair(nType, lpGN));
it = mapGlideNum.find(nType);
}
_mtx.unlock(); //解锁
static long NUM_SPACE = 100; //每次增长的大小
Locker locker(*it->second->_mutex); //相当于TLock类里的TGuard
if(it->second->lCount != 0 && it->second->lCount < NUM_SPACE)
{
it->second->lCount++;
it->second->lNumber++; //序列号值加1
return it->second->lNumber;
}
else //超过了增长的范围
{
//1.更新数据库的序列号数字上限
//Update GlideNumber Set MaxNum = MaxNum + %d where keyType = %d, NUM_SPACE, nType;
//返回之前未扩容时的Number值
//Select MaxNumber - %d as Number from GlideNumber where keyType = %d, NUM_SPACE, nType
it->second->lCount = 1;
return it->second->second->lNumber;
}
}