-
-
[旧帖] [求助]这段代码哪里错了,不是编译错误,是逻辑错误。 0.00雪花
-
发表于: 2011-1-7 16:59 990
-
#include <iostream>
#include <string>
#include <Afxmt.h>
#include <vector>
using namespace std;
vector<string> temp;
CSemaphore semaphoreWrite(1,1);
CCriticalSection critical_section;
UINT Run(LPVOID lp)
{
int i = 0;
while(i<100)
{
critical_section.Lock();
string tem;
char sp[1024];
sprintf(sp,"127.0.0.%d",i);
tem = sp;
temp.push_back(tem);
i++;
critical_section.Unlock();
Sleep(3000);
}
return 1;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
AfxBeginThread(Run,NULL);
vector<string>::iterator p = temp.begin();
while(1)
{
critical_section.Lock();
for(;p != temp.end();p++)
{
cout<<p->c_str()<<endl;
}
p = temp.begin();
critical_section.Unlock();
Sleep(100);
}
return 0;
}
希望看雪的各位高手帮帮忙!谢谢了!
#include <string>
#include <Afxmt.h>
#include <vector>
using namespace std;
vector<string> temp;
CSemaphore semaphoreWrite(1,1);
CCriticalSection critical_section;
UINT Run(LPVOID lp)
{
int i = 0;
while(i<100)
{
critical_section.Lock();
string tem;
char sp[1024];
sprintf(sp,"127.0.0.%d",i);
tem = sp;
temp.push_back(tem);
i++;
critical_section.Unlock();
Sleep(3000);
}
return 1;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
AfxBeginThread(Run,NULL);
vector<string>::iterator p = temp.begin();
while(1)
{
critical_section.Lock();
for(;p != temp.end();p++)
{
cout<<p->c_str()<<endl;
}
p = temp.begin();
critical_section.Unlock();
Sleep(100);
}
return 0;
}
希望看雪的各位高手帮帮忙!谢谢了!
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
看原图
赞赏
雪币:
留言: