69,373
社区成员
发帖
与我相关
我的任务
分享
#include <afxwin.h>
#include <cstringt.h>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <thread>
std::string MyGetTime()
{
CString strTime; //用于将CTime对象格式化为字符串
CTime curTime = CTime::GetCurrentTime(); //获取当前的系统时间
int nYear = curTime.GetYear();
int nMonth = curTime.GetMonth();
int nDay = curTime.GetDay();
int nHour = curTime.GetHour();
int nMin = curTime.GetMinute();
int nSec = curTime.GetSecond();
strTime = curTime.Format(_T("%Y%m%d%H%M%S"));
return strTime.GetString();
}
void GetSeqNo(std::string& _sNewSeqNo)
{
static int nSeqNo = 0; /*用于每次自增的序列号*/
nSeqNo > 99999 ? nSeqNo = 0 : nSeqNo++;
std::string sCurrentTime = MyGetTime(); /*获取当前时间*/
/*开始拼接流水号*/
char pszTempSeq[64 + 1];
memset(pszTempSeq,0x00,sizeof(pszTempSeq));
snprintf(pszTempSeq,sizeof(pszTempSeq),"%s%05d", sCurrentTime.c_str(), nSeqNo);
_sNewSeqNo = pszTempSeq;
}
void threadTest()
{
std::string sNewSeqNo;
GetSeqNo(sNewSeqNo);
printf("当前线程: %lu , 新的序列号: %s\n", GetCurrentThreadId(), sNewSeqNo.c_str());
return;
}
int main(void)
{
for (int ii = 0; ii < 100; ii++)
{
std::thread t1(threadTest);
t1.detach();
}
system("pause");
return 0;
}