“节目意外结束了。”
我有一堂课正在打电话CMem::Write()
。并将迭代显示到屏幕上。有时它会达到 140,其他... 12、3、42,立即掉出...非常随机。
如果我删除对 的调用 CMem::Write()
,程序将永远运行。
不知道为什么程序终止?我只能假设某些东西不是写在CMem::Write()
方法中的。
CMem::CMem()//sets up the "static" stack memory and the pointers to it; also the "static" positionIndex
{
m_nBufferLength = sizeof(char); //short int
static char *cMessageCB = new char[m_nBufferLength];
static double *dTimeCB = new double[m_nBufferLength];
m_cMessageCB = cMessageCB;
m_dTimeCB = dTimeCB;
////////////////////////////////////////
static char *cMessageReadList = new char[m_nBufferLength]; //max size can be the CB
static double *dTimeReadList = new double[m_nBufferLength]; //max size can be the CB
m_cMessageReadList = cMessageReadList;
m_dTimeReadList = dTimeReadList;
static int firstInstance = 0;
if(firstInstance == 0){
m_posRead = 0;//only on first instance
m_posWrite = 0;//only on first instance
firstInstance++;//check to see if multiple threads entered at the same time and look at the count
}
}
void CMem::Write()
{//double dTime, char cMessage
//only one thread can write at a time... so lock... (make other threads with various random delays)
static bool bUse = false;
bool bDone = false;
while(bDone == false){
if(bUse == false){
bUse = true;
m_cMessageCB[m_posWrite] = m_cMessageWrite;
m_dTimeCB[m_posWrite] = m_dTimeWrite;
m_posWrite = (unsigned char)(m_posWrite + 1);
static char cFlag = 0;
//if writing position == reading position then flag
if(m_posWrite == m_posRead){
cFlag = 1;
}
bDone = true;
bUse = false;
}else if(bUse == true){
printf("SUSPEND ");
}
}
}
void CMem::Read()
{//get the whole block of memory and increment the m_posRead accordingly
unsigned char j = 0;
while( (m_posRead + 1) != (m_posWrite + 1) ){
m_cMessageReadList[j] = m_cMessageCB[m_posRead];//inc m_posRead at the end
m_dTimeReadList[j] = m_dTimeCB[m_posRead];//inc m_posRead at the end
m_posRead = (unsigned char)(m_posRead + 1);//circulate around
j++;// 'j' is not circulating back around
}
//write to file
}