Click here to Skip to main content
15,848,580 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
Randor 22-Nov-16 13:27
professional Randor 22-Nov-16 13:27 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
David Crow22-Nov-16 13:48
David Crow22-Nov-16 13:48 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
leon de boer22-Nov-16 20:15
leon de boer22-Nov-16 20:15 
QuestionRun time validation in .RC file in VC++ Mfc application Pin
rajmohanpatel22-Nov-16 0:50
rajmohanpatel22-Nov-16 0:50 
AnswerRe: Run time validation in .RC file in VC++ Mfc application Pin
Jochen Arndt22-Nov-16 1:11
professionalJochen Arndt22-Nov-16 1:11 
QuestionTCP server handle Clients, std::map or array? Pin
bestbear20-Nov-16 6:03
bestbear20-Nov-16 6:03 
AnswerRe: TCP server handle Clients, std::map or array? Pin
Afzaal Ahmad Zeeshan20-Nov-16 22:36
professionalAfzaal Ahmad Zeeshan20-Nov-16 22:36 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear20-Nov-16 23:23
bestbear20-Nov-16 23:23 
Ok,let me explain it in details

1 I have to maintain a collection for all the clients data,such as

C++
struct CLIENT_DATA
{
std::string m_strIp;
int m_nOnlineTime;
int m_nCredits;
};


2 the client data update(by a timer for example) and the socket are in different thread

so If I use a map

C++
bool Connect(std::string strIp)
{
   boost::lock_guard<boost::mutex> lock(m_lock);

  CLIENT_DATA* pData = new CLIENT_DATA;
  pData->m_nOnlineTime = 0;
  pData->m_strIp = strIp;
  m_mapClients.push_back(strIp,pData);
  
  return true;
}

void Disconnect(std::string strIp);
{
 	boost::lock_guard<boost::mutex> lock(m_lock);

        std::map<std::string,CLIENT_DATA*>::iterator iter = m_mapClients.find(strIp);
	if(iter != m_mapClients.end())
		return;

	CLIENT_DATA* pClientData = m_mapClients[strIp];
	if(pClientData != NULL)
        {
           delete pClientData;
           pClientData = NULL;
        }
        m_mapClients.erase(iter);
}

bool UpdateOnlineTime(std::string strIp)
{
 	boost::lock_guard<boost::mutex> lock(m_lock);
	
        std::map<std::string,CLIENT_DATA*>::iterator iter = m_mapClients.find(strIp);
  	if(iter != m_mapClients.end())
		return false;

	CLIENT_DATA* pClientData = m_mapClients[strIp];
	if(pClientData == NULL)
               return false;
	pClientData->m_nOnlineTime += 10;
	
	return true;
}


I have to lock the map use the same mutex in connect,disconnect,updateonlinetime

if I use a array

C++
void PrepareIds()
{
	for(int i=0;i<10240;i++)
	   m_vecAvailableIds.push_back(i);
	
}

int FetchId()
{
	boost::lock_guard<boost::mutex> lock(m_lock);
	if(m_vecAvailableIds.size()<=0)
            return -1;

	int nId =  m_vecAvailableIds.back();
	m_vecAvailableIds.pop_front();
        return nId;
}

int ReturnId(int nId)
{
      boost::lock_guard<boost::mutex> lock(m_lock);
      m_vecAvailableIds.push_back(nId);
}

bool Connected(std::string strIp)
{
	int nNewId = FetchId();
	if(nNewId == -1)
             return false;

	CLIENT_DATA* pData = m_arrClients[nNewId];
        boost::lock_guard<boost::mutex> lock(pData->m_mutex_lock);
        pData->m_bInUse = true;
	pData->m_nOnlineTime = 0;	
	pData->m_strIP = strIp;
}

bool Disconnected(int nClientId)
{
	Assert(nClientId>=0 && nClientId<10240);
	
        CLIENT_DATA* pData = m_arrClients[nClientId];
        boost::lock_guard<boost::mutex> lock(pData->m_mutex_lock);
        pData->m_bInUse = false;

        ReturnId(nClientId);

	return true;
}


Now,the UpdateOnlineTime function and the CLIENT_DATA structure are changed


C++
struct CLIENT_DATA
{
std::string m_strIp;
int m_nOnlineTime;
int m_nCredits;
bool m_bInUse;
boost::mutex m_mutex_data;
};

bool UpdateOnlineTime(int nClientId)
{
	Assert(nClientId>=0 && nClientId<10240);
	
	CLIENT_DATA* pData = m_arrClients[nClientId];
	
 	boost::lock_guard<boost::mutex> lock(pData->m_mutex_lock);
	if(! pClientData->m_bIsInUse)
             return false;
	
	pClientData->m_nOnlineTime += 10;
	
	return true;
}


There is only need to lock one CLIENT_DATA in case we may update the m_nCredits in some other place.
Now there are two kinds of mutex,one for Ids,one for each CLIENT_DATA

the most important point is when I iterate all the client datas
if I use a map:

C++
UpdateClients()
{
    boost::lock_guard<boost::mutex> lock(m_lock);
   for(int i=0;i<m_mapClients.size();i++)
   {

      Update();
    }
}


if I use an array
C++
UpdateClients()
{
    //there is no need to lock
    for(int i=0;i<m_arrClients.size();i++)
   {

    Update();
    }
}


if the map size if very large,such as 10240;
lock the whole map would block the connect or disconnect operation.

modified 21-Nov-16 5:35am.

AnswerRe: TCP server handle Clients, std::map or array? Pin
Jochen Arndt21-Nov-16 1:22
professionalJochen Arndt21-Nov-16 1:22 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear21-Nov-16 2:11
bestbear21-Nov-16 2:11 
GeneralRe: TCP server handle Clients, std::map or array? Pin
Jochen Arndt21-Nov-16 3:35
professionalJochen Arndt21-Nov-16 3:35 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear21-Nov-16 3:48
bestbear21-Nov-16 3:48 
QuestionSimple WaitForSingleObject question Pin
ForNow19-Nov-16 17:14
ForNow19-Nov-16 17:14 
AnswerRe: Simple WaitForSingleObject question Pin
Midi_Mick19-Nov-16 18:09
professionalMidi_Mick19-Nov-16 18:09 
GeneralRe: Simple WaitForSingleObject question Pin
ForNow20-Nov-16 11:48
ForNow20-Nov-16 11:48 
AnswerRe: Simple WaitForSingleObject question Pin
Midi_Mick20-Nov-16 15:17
professionalMidi_Mick20-Nov-16 15:17 
QuestionComponent "Assimp" and Export 3D Scene into Graphical File Pin
Onic77719-Nov-16 12:47
Onic77719-Nov-16 12:47 
AnswerRe: Component "Assimp" and Export 3D Scene into Graphical File Pin
leon de boer19-Nov-16 20:17
leon de boer19-Nov-16 20:17 
Questionpkg_config ?? Pin
Vaclav_18-Nov-16 15:27
Vaclav_18-Nov-16 15:27 
AnswerRe: pkg_config ?? Pin
Richard MacCutchan18-Nov-16 22:55
mveRichard MacCutchan18-Nov-16 22:55 
GeneralRe: pkg_config ?? Pin
Jochen Arndt18-Nov-16 23:54
professionalJochen Arndt18-Nov-16 23:54 
GeneralRe: pkg_config ?? Pin
Richard MacCutchan19-Nov-16 2:21
mveRichard MacCutchan19-Nov-16 2:21 
GeneralRe: pkg_config ?? Pin
Vaclav_19-Nov-16 10:29
Vaclav_19-Nov-16 10:29 
GeneralRe: pkg_config ?? Pin
Richard MacCutchan19-Nov-16 13:03
mveRichard MacCutchan19-Nov-16 13:03 
AnswerRe: pkg_config ?? Pin
markkuk21-Nov-16 0:39
markkuk21-Nov-16 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.