Click to See Complete Forum and Search --> : Clarification regarding memory management in multi thread


s_rs
July 18th, 2005, 06:11 AM
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

Armen
July 18th, 2005, 07:14 AM
Originally posted by s_rs

Threoritically memory allocated in one thread using malloc / new function can not be freed using free / delete in other thread. It it correct?


Not it isn't correct.
Memory alocated using malloc / new in one thread within the same process can be deleted in another thread, just your threads need to know the address of memory, so you need to use global pointer or some mechanism to be able to get the address of alocated memory.
And sure accessing allocated global memory from multiple threads is not thread safe, so probably you will need the synchronization using for example Critical Sections.
In your example you use EnterCriticalSection/LeaveCriticalSection pairs in one thread, you need it in any thread that deals with your global data.

NMTop40
July 19th, 2005, 07:12 AM
If you are sending messages to waiting threads in Win32 API, a good method to use is the function QueueUserAPC(). (This is like a remote-procedure call but for threads, i.e. it instructs a thread to call a procedure).

Threads usually have their own alllocation of stack but share a common heap. (However you could use QueueUserAPC() to a heap-managing thread if you insist to allocate and deallocate objects).

Often QueueUserAPC() is used to generate a "workflow" situation, i.e. each thread sits doing one particular task and is "in charge" of that task. Therefore, you put one thread in charge of a section of memory (a list of items or whatever) and use the "workflow" situation on that thread to handle the tasks.