Clarification regarding memory management in multi thread
-
Hello All, There is a basic doubt in multi threading. I want to allocate memory in one thread and i want to use that memory in other thread and free in that thread. Threoritically memory allocated in one thread using malloc / new function can not be freed using free / delete in other thread. It it correct? The code I have given below Language VC++ 6.0 OS: Windows 2000
typedef struct CapturedData { long m_lLength; char *m_pData; }NewCapturedData; // Global variable to store the data CTypedPtrList g_oCapDataList; // First Thread Function LRESULT OnCaptureAddData (WPARAM wParam, LPARAM lParam) { NewCapturedData *l_pCapData; l_pCapData = new NewCapturedData; l_pCapData->m_lLength = (long) lParam; l_pCapData->m_pData = (char*) calloc (sizeof (char), (long)lParam); if (!l_pCapData->m_pData) { return FALSE; } memcpy (l_pCapData->m_pData, (char*) wParam, (long)lParam); EnterCriticalSection (&m_oCapDataCriticalSession); g_oCapDataList.AddTail (l_pCapData); LeaveCriticalSection (&m_oCapDataCriticalSession); PostThreadMessage (WM_CAPTURE_PROCESS_DATA, 0, 0); return TRUE; } // Second Thread Function LRESULT OnProcessDataMsg (WPARAM wParam, LPARAM lParam) { CapturedData *l_pCapData = g_oCapDataList.GetHead (); // Do some ananlysis with data. free (l_pCapData->m_pData); l_pCapData->m_pData = NULL; free (l_pCapData); l_pCapData = NULL; }
NOTE: Above code is part of code i have given, but orginal code is compiling and linking and executing. The project is dealing with packet capturing and it has to take minimum memory and really fast. Is it better idea to free in the second thread? If the memory has to be freed in first thread? I am planning to post a message to first thread, to intimate first thread from 2nd thread when ananysis is complated to remove the memory!!!! For this approach i have to add "GetMessage" in First thread and depending on the message free the memory. Can you please suggest which design is better? Thanks for your time Ravi -
Hello All, There is a basic doubt in multi threading. I want to allocate memory in one thread and i want to use that memory in other thread and free in that thread. Threoritically memory allocated in one thread using malloc / new function can not be freed using free / delete in other thread. It it correct? The code I have given below Language VC++ 6.0 OS: Windows 2000
typedef struct CapturedData { long m_lLength; char *m_pData; }NewCapturedData; // Global variable to store the data CTypedPtrList g_oCapDataList; // First Thread Function LRESULT OnCaptureAddData (WPARAM wParam, LPARAM lParam) { NewCapturedData *l_pCapData; l_pCapData = new NewCapturedData; l_pCapData->m_lLength = (long) lParam; l_pCapData->m_pData = (char*) calloc (sizeof (char), (long)lParam); if (!l_pCapData->m_pData) { return FALSE; } memcpy (l_pCapData->m_pData, (char*) wParam, (long)lParam); EnterCriticalSection (&m_oCapDataCriticalSession); g_oCapDataList.AddTail (l_pCapData); LeaveCriticalSection (&m_oCapDataCriticalSession); PostThreadMessage (WM_CAPTURE_PROCESS_DATA, 0, 0); return TRUE; } // Second Thread Function LRESULT OnProcessDataMsg (WPARAM wParam, LPARAM lParam) { CapturedData *l_pCapData = g_oCapDataList.GetHead (); // Do some ananlysis with data. free (l_pCapData->m_pData); l_pCapData->m_pData = NULL; free (l_pCapData); l_pCapData = NULL; }
NOTE: Above code is part of code i have given, but orginal code is compiling and linking and executing. The project is dealing with packet capturing and it has to take minimum memory and really fast. Is it better idea to free in the second thread? If the memory has to be freed in first thread? I am planning to post a message to first thread, to intimate first thread from 2nd thread when ananysis is complated to remove the memory!!!! For this approach i have to add "GetMessage" in First thread and depending on the message free the memory. Can you please suggest which design is better? Thanks for your time RaviRavi Sankar S wrote: Threoritically memory allocated in one thread using malloc / new function can not be freed using free / delete in other thread. It it correct? No, that is not correct.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown