我正在尝试使用地图系统来存储和更新聊天服务器的数据。应用程序是mutlithreaded并使用锁定系统来防止多个线程访问数据。
问题在于:当从地图中单独删除客户端时,就可以了。但是,当我尝试调用多个关闭时,它会在内存中留下一些。如果我在地图上调用:: clear(),它会导致调试断言错误“Iterator not compatible”或类似错误。该代码将首次运行(使用80多个控制台作为测试连接进行测试),但由于它留下了大块,因此无法再次运行。我尝试过研究方法,并且已经编写了系统来停止代码执行,直到每个进程完成。到目前为止,我感谢任何帮助,并附上了相关的代码片段。
//portion of server code that handles shutting down
DWORD WINAPI runserver(void *params) {
runserverPARAMS *p = (runserverPARAMS*)params;
/*Server stuff*/
serverquit = 0;
//client based cleanup
vector<int> tokill;
map<int,int>::iterator it = clientsockets.begin();
while(it != clientsockets.end()) {
tokill.push_back(it->first);
++it;
}
for(;;) {
for each (int x in tokill) {
clientquit[x] = 1;
while(clientoffline[x] != 1) {
//haulting execution until thread has terminated
}
destoryclient(x);
}
}
//client thread based cleanup complete.
return 0;
}
//clientioprelim
DWORD WINAPI clientioprelim(void* params) {
CLIENTthreadparams *inparams = (CLIENTthreadparams *)params;
/*Socket stuff*/
for(;;) {
/**/
}
else {
if(clientquit[inparams->clientid] == 1)
break;
}
}
clientoffline[inparams->clientid] = 1;
return 0;
}
int LOCKED; //exported as extern via libraries.h so it's visible to other source files
void destoryclient(int clientid) {
for(;;) {
if(LOCKED == 0) {
LOCKED = 1;
shutdown(clientsockets[clientid], 2);
closesocket(clientsockets[clientid]);
if((clientsockets.count(clientid) != 0) && (clientsockets.find(clientid) != clientsockets.end()))
clientsockets.erase(clientsockets.find(clientid));
if((clientname.count(clientid) != 0) && (clientname.find(clientid) != clientname.end()))
clientname.erase(clientname.find(clientid));
if((clientusername.count(clientid) != 0) && (clientusername.find(clientid) != clientusername.end()))
clientusername.erase(clientusername.find(clientid));
if((clientaddr.count(clientid) != 0) && (clientaddr.find(clientid) != clientaddr.end()))
clientaddr.erase(clientusername.find(clientid));
if((clientcontacts.count(clientid) != 0) && (clientcontacts.find(clientid) != clientcontacts.end()))
clientcontacts.erase(clientcontacts.find(clientid));
if((clientquit.count(clientid) != 0) && (clientquit.find(clientid) != clientquit.end()))
clientquit.erase(clientquit.find(clientid));
if((clientthreads.count(clientid) != 0) && (clientthreads.find(clientid) != clientthreads.end()))
clientthreads.erase(clientthreads.find(clientid));
LOCKED = 0;
break;
}
}
return;
}
答案 0 :(得分:0)
您是否真的使用int
进行锁定,还是仅仅是对代码的简化?如果你真的使用int
:如果两个线程在分配变量之前检查变量(简化),那么这将无法同时输入两次(或更多)临界区。请参阅mutexes in Wikipedia以供参考。您可以使用由Windows提供的某种互斥锁,也可以使用boost thread而不是int
。