2

我正在尝试使用地图系统来存储和更新聊天服务器的数据。该应用程序是多线程的,并使用锁定系统来防止多个线程访问数据。

问题是这样的:当从地图中单独删除客户端时,没关系。但是,当我尝试调用多个关闭时,它会在内存中留下一些。如果我在地图上的任何时候调用 ::clear() ,它会导致调试断言错误,出现“迭代器不兼容”或类似错误。该代码将在第一次工作(使用 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;
}
4

1 回答 1

0

你真的使用intfor 锁定还是只是代码的简化?如果您真的使用int: 这将不起作用,并且可以同时输入两次(或更多)关键部分,如果两个线程在一个分配给它之前检查变量(简化)。请参阅Wikipedia 中的互斥锁以供参考。您可以使用 windows 提供的某种互斥锁,也可以使用boost 线程而不是int.

于 2012-05-27T02:28:58.647 回答