|
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 {<br />
long m_lLength;<br />
char *m_pData;<br />
}NewCapturedData;<br />
<br />
CTypedPtrList <CPtrList, NewCapturedData*> g_oCapDataList;<br />
<br />
LRESULT OnCaptureAddData (WPARAM wParam, LPARAM lParam) <br />
{<br />
NewCapturedData *l_pCapData;<br />
<br />
l_pCapData = new NewCapturedData;<br />
l_pCapData->m_lLength = (long) lParam;<br />
l_pCapData->m_pData = (char*) calloc (sizeof (char), (long)lParam);<br />
if (!l_pCapData->m_pData) {<br />
return FALSE;<br />
}<br />
memcpy (l_pCapData->m_pData, (char*) wParam, (long)lParam);<br />
EnterCriticalSection (&m_oCapDataCriticalSession);<br />
g_oCapDataList.AddTail (l_pCapData);<br />
LeaveCriticalSection (&m_oCapDataCriticalSession);<br />
PostThreadMessage (WM_CAPTURE_PROCESS_DATA, 0, 0);<br />
return TRUE;<br />
}<br />
<br />
LRESULT OnProcessDataMsg (WPARAM wParam, LPARAM lParam)<br />
{<br />
CapturedData *l_pCapData = g_oCapDataList.GetHead ();<br />
<br />
<br />
free (l_pCapData->m_pData);<br />
l_pCapData->m_pData = NULL;<br />
free (l_pCapData);<br />
l_pCapData = NULL;<br />
}<br />
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
|
|
|
|
|
Ravi 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
|
|
|
|
|
....
My application draws a continous graph in a picture box treating it as a bitmap of width 600 pixels...once the area is filled...BitBlt function scrolls the graph towards left in order to continue drawing...problem is that if I try to scroll the graph MORE THAN 40 PIXELS .drawing area is not cleared properly....
what should be done
|
|
|
|
|
Post the code, so we can tell what the problem is.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Raza5680 wrote:
problem is that if I try to scroll the graph MORE THAN 40 PIXELS .drawing area is not cleared properly....
Just a guess:
You need to invalidate the drawing area.
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
I want to delete a file,but MFC shows shareviolet error
how can disable this error and delete that file?
|
|
|
|
|
A file's color should have no bearing on whether it can be deleted or not.
A sharing violation indicates that the file is in use by another process. Are you sure you still want to delete it?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
How can i resize a dialog to fullscreen
|
|
|
|
|
int x;
int y;
x = GetSystemMetrics(SM_CXSCREEN);
y = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(NULL,0,0,x,y,SWP_NOZORDER);
this is this.
|
|
|
|
|
I want to get the workspaces path.GetCommandLine() only displays
the ....\debug\*.exe path.Please how can i get the *.dsw path where
the source files are kept
|
|
|
|
|
use "GetCurrentDirectory"
|
|
|
|
|
Can you tell me the most simplest way to change the CButton's back ground color.
|
|
|
|
|
|
Handle the WM_CTLCOLOR message in your Dialog's class.
Einstein: "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."
My Articles
|
|
|
|
|
i want to create the file using Cfile with the date i given.....
please kindly reply soon......
ditty grail
|
|
|
|
|
You can't specify a date, but there are classes in MFC that allow you to set it afterwards. Damn[^], I thought it was seperate class, but it's a static method on the CFile class.
Which denies me the right to comment on the standard library having (overall) better file management mechanisms....
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Can some one explain how the arc is created by CDC::Arc method . I just went through the MSDN documentation but cud not get it. Please help me. I will be thankful to him/her
|
|
|
|
|
MFC includes the full source. Here it is:
BOOL CDC::ArcTo(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
ASSERT(m_hDC != NULL);
BOOL bResult = ::ArcTo(m_hDC, x1, y1, x2, y2, x3, y3, x4, y4);
if (m_hDC != m_hAttribDC)
{
CPoint pt;
VERIFY(::GetCurrentPositionEx(m_hDC, &pt));
VERIFY(::MoveToEx(m_hAttribDC, pt.x, pt.y, NULL));
}
return bResult;
}
As you can see, it calls an internal API function, as much of the MFC GDI classes do. I suggest buying a maths book on the topic if you want to know more.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Well Thanx but the source code is of method "ArcTo" ... From where can I get source code of Arc ... Help me Please
|
|
|
|
|
Well I want to know more ... So i shud buy a math book :P
|
|
|
|
|
I created a row socket in order to send raw TCP data using following code:
SOCKET sRaw = socket(AF_INET,SOCK_RAW,IPPROTO_IP);
int flag = 1;
setsockopt(sRaw, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeo(flag));
after i intialized buffer and dest address, i call sendto function send it:
int nRet = ::sendto(sRaw,
buff, sizeof(ipHeader) + sizeof(tcpHeader), 0, (sockaddr*)&in, sizeof(in));
if(nRet == SOCKET_ERROR)
{
printf(" sendto failed () %d \n", ::GetLastError());
return;
}
the result is sendto failed with error code : 10004.
Why? help me, tell me why, how can i send raw TCP data using raw socket.
my OS is Windows xp sp2.
|
|
|
|
|
Microsoft have crippled the TCP implementation in SP2. Google groups is your friend.
Aaron.
|
|
|
|
|
thank you very much.
does TCP implementation exist in SP1?
|
|
|
|
|
Hello All,
I have a assignment something like this
LPTHREAD_START_ROUTINE a = ThreadProc;
Where ThreadProc is the thread function
After the above assignment a and ThreaProc shows different pointer value. Which is understandable.
My question is how do I get the ThreadProc pointer value back from a. How should my variable declaration look like
i.e if ThreadProc shows 0x00401028 in the watch window
and a shows 0x00401c90
then how do I obtain back 0x00401028 from a.
Rajdeep
|
|
|
|
|
rajandpayal wrote:
I have a assignment something like this
LPTHREAD_START_ROUTINE a = ThreadProc;
Where ThreadProc is the thread function
After the above assignment a and ThreaProc shows different pointer value. Which is understandable.
i.e if ThreadProc shows 0x00401028 in the watch window
and a shows 0x00401c90
You saw that in a Debug build, right? In a Release build, a will most likely have the direct address of ThreadProc .
rajandpayal wrote:
then how do I obtain back 0x00401028 from a
In a Debug build, a will point to a JMP to ThreadProc instruction. You might inspect the memory pointed by a to obtain the JMP instruction (which comprises 5 bytes, if I remember well), and then decode the parameter, which would be the ThreadProc address...
But, I'm curious, why would you need that?
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|