• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

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


John paul coder

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))
if (NULL == getCallObjectOfThisID(l_hStrCallID))
//create Process object....
l_hProcessObj = createProcessObject(m_strSoftPhoneName,l_hStrCallID,true,false);

if (NULL != m_hlstCallList)

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();


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:

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...
Top Bottom