Jump to content
Xtreme .Net Talk

How to handle the situation where shared data (shared memory) using raw pointers being used in two separate threads in C++?


Recommended Posts

Guest John paul coder
Posted

Hello All,

 

Could someone please help me, how to handle the situation where the same shared data (shared memory) using raw pointers being used in two separate threads?

 

Below is the code snippet and the detailed explanation of the scenario:

 

Here in the below function, we use the l_hNewProcessObj which is created. And this is the raw pointer

CTestImpl* CMyImpl::createProcessObject(string f_hStrSFID, string f_nCallID,

bool f_boReceiveCall)

{

CTestImpl * l_hNewProcessObj = NULL;

ReturnCode l_eReturnCode = RETURNCODE_SUCCESS;

 

l_hNewProcessObj = new (std::nothrow) CTestImpl(f_nCallID, f_hStrSFID, f_boReceiveCall,

m_nVSFSocketHandle, this);

return l_hNewProcessObj;

}

 

And in the below handleTestEvents function we are using this createProcessObject function

 

ReturnCode CMyImpl::handleTestEvents(CXMLObject* f_hMsgObj)

{

string l_hStrConfCallID = f_hMsgObj->getConfCallID();

string l_hStrCallID = f_hMsgObj->getCallID();

 

string l_StrMessageName = f_hMsgObj->getMessgeName();

ReturnCode l_eReturnCode = RETURNCODE_SUCCESS;

if(strcmp(l_hStrConfCallID.c_str(), "0") != 0

&& strcmp(l_hStrConfCallID.c_str(), "") != 0)

{

l_hStrCallID = l_hStrConfCallID;

}

 

CTestImpl *l_hProcessObj = NULL;

 

if (0 == l_StrMessageName.compare(REQ_XML_MESSAGE_CALL)

|| 0 == l_StrMessageName.compare(REQ_XML_MESSAGE_FIRST_CALL))

{

if (0 == strcmp(l_StrMessageName.c_str(),REQ_XML_MESSAGE_FIRST_CALL))

{

m_hEventListener->setDeviceStatus(true);

if (NULL == getCallObjectOfThisID(l_hStrCallID))

//create Process object....

{

l_hProcessObj = createProcessObject(m_strSoftPhoneName,l_hStrCallID,true,false);

}

if (NULL != m_hlstCallList)

{

m_hlstCallList->push_back((l_hProcessObj));

}

m_hEventListener->handleAllEvents(MSG_SP_CALL,

static_cast<ICall*>(l_hProcessObj));

}

}

 

In the above function, the createProcessObject is allocating the pointer

And this is getting pushed into the list

And then this is calling the below handleAllEvents function.

 

void CTestManager::handleAllEvents(Events f_MyEvent,

EMRCall::ICall* f_hProcessObject)

{

Message* l_hMessage = new Message();

l_hMessage->m_eEvents = f_MyEvent;

l_hMessage->m_hProcessObj = f_hProcessObject;

 

if(nullptr != f_hProcessObject)

{

l_hMessage->m_hStrSoftphone = f_hProcessObject->getDeviceID();

}

m_hMessageQueueObj->postMsg(l_hMessage);

}

 

Here in CTestManager also f_hProcessObject is a raw pointer and it is getting assigned here.

This raw pointer is also used in the list in the CTestImpl

Now this message is posted to the message queue using the statement:

m_hMessageQueueObj->postMsg(l_hMessage);

 

Here we have a kind of situation where we will have a Processobject kind of shared data which is passed from the CMyImpl to the CTestManager as a raw pointer.

So you have the same memory which is pointed to by the processobject data being used in two separate threads

This is the kind of contention between the shared memory which should be handled properly between two threads

How we are going to handle this?

 

Could some one please help me how to handle this kind of situation.

 

I have few options in mind like as shown below:

 

1. You can’t control the same memory from two different threads until you have critical section locks before changing the state of process object and protected before reading from it

 

2. Other option is you create a copy of the object. Only pass the copy to the other side

Because we are not going to write anything to the object We are only reading the information

 

3. The other option is go with the thread less architecture wherein you don’t have threads

So that at a time one can access it

 

Please provide your valuable inputs and suggestions to proceed further.

 

Thanks in advance.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...