The existing network communication module, is composed of three threads:
1. Receiving threads recvThread is responsible for the overall connection handle handle receiving data
2. Send thread sendThread responsible for sending data to global connection handle handle
3. Disconnect reconnection threads reconnThread is responsible for the connection failure to reconnect and refresh the global connection handle handle
Connection handle handle support handle. Recv and handle the send, recv and send immediately when the network abnormal return 1,
Thread function pseudo code below
RecvThread () {
While (true) {
Ret=handle. Recv (& amp; The data);
If (ret==1) {
Reconnet ();
Sleep (1);
continue;
}
DataProc (data);
}
}
SendThread () {
While (true) {
Data=(https://bbs.csdn.net/topics/someProc);
Ret=handle. Send (data);
If (ret==1) {
Reconnet ();
Sleep (1);
continue;
}
}
}
Reconnet () {
//start a separate thread running reconnThread
StartThread (reconnThread);
}
ReconnThread () {
Reconnect (& amp; Handle);
}
1. Please use natural language to describe how the code above the synchronous mutex problem,
2. Please use the above code modifications, solve the problem of synchronous mutex,
CodePudding user response:
This kind of situation really consider directly using a circular queue, set the read/write pointer, ensure the read pointer always lag behind the write pointer can, no lock or other synchronous mutex, efficiency is very high,