A question about SendMessage and PostMessage
-
Is it memory safe to use
SendMessage(PostMessage)
to pass parameters between threads? There are two threads A and B; // thread A want to send some message to thread B code sample about thread A:{ char sz[] = "Hello, CodeProject"; BOOL b = TRUE; SendMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, (LPRARM)b); //or PostMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, 0); }
Can I write code like this in thread B to retriecve the message?{ MSG msg GetMessage(&msg, hWnd, 0, 0); char* p = (char*)(msg.wParam) BOOL b = (BOOL)(msg.lParam) cout<<p<<endl; // can I get "Hello, CodeProject"? }
Thank you for your reply. :) -
Is it memory safe to use
SendMessage(PostMessage)
to pass parameters between threads? There are two threads A and B; // thread A want to send some message to thread B code sample about thread A:{ char sz[] = "Hello, CodeProject"; BOOL b = TRUE; SendMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, (LPRARM)b); //or PostMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, 0); }
Can I write code like this in thread B to retriecve the message?{ MSG msg GetMessage(&msg, hWnd, 0, 0); char* p = (char*)(msg.wParam) BOOL b = (BOOL)(msg.lParam) cout<<p<<endl; // can I get "Hello, CodeProject"? }
Thank you for your reply. :)Neither technique is thread safe: in both cases,
sz
is local to your function and will be destroyed when the function terminates. It means that in the other thread, dependings of how the threads were processed, you may access memory that has been freed. That's what is called a 'race condition'. It depends how the two threads were processed and sometimes it will work fine and other times, it will fail.Cédric Moonen Software developer
Charting control [v1.2] -
Neither technique is thread safe: in both cases,
sz
is local to your function and will be destroyed when the function terminates. It means that in the other thread, dependings of how the threads were processed, you may access memory that has been freed. That's what is called a 'race condition'. It depends how the two threads were processed and sometimes it will work fine and other times, it will fail.Cédric Moonen Software developer
Charting control [v1.2]Cedric Moonen wrote:
sz is local to your function and will be destroyed when the function terminates.
Thank you Cédric Moonen :) So , if I want to use
SendMessage(PostMessage)
to send some more information than WPARAM and LPRAM, I should usenew
operator to allocate memory on the heap, and after GetMessage returned usedelete
to free memory? -
Cedric Moonen wrote:
sz is local to your function and will be destroyed when the function terminates.
Thank you Cédric Moonen :) So , if I want to use
SendMessage(PostMessage)
to send some more information than WPARAM and LPRAM, I should usenew
operator to allocate memory on the heap, and after GetMessage returned usedelete
to free memory?Yes, in that case it is safe (even if it is not beautifull ;P ).
Cédric Moonen Software developer
Charting control [v1.2] -
Yes, in that case it is safe (even if it is not beautifull ;P ).
Cédric Moonen Software developer
Charting control [v1.2]not beautiful? :sigh: Is there some beautiful ways to send large objects between threads? How about Memory-mapped files? I think it is not beautiful too. Because after you copy some information in the memory-mapped file, you still need to tell the other thread to retrieve the information. Thank you! :)
-
not beautiful? :sigh: Is there some beautiful ways to send large objects between threads? How about Memory-mapped files? I think it is not beautiful too. Because after you copy some information in the memory-mapped file, you still need to tell the other thread to retrieve the information. Thank you! :)
When I said not beautifull, it is mainly because in that case your second thread will need a message loop so it is not applicable in all cases. If you have already a message loop in your second thread then it is fine. There are, I think, better solutions but it all depends of what you want to achieve. What I do in general is having a class ta wraps the threading functionality (have a member function that will be called from the thread) and then if I need to 'exchange' information, I'll do that through class members and I use events to notify the other thread and eventually critical sections to protect the access to the shared resources.
Cédric Moonen Software developer
Charting control [v1.2] -
When I said not beautifull, it is mainly because in that case your second thread will need a message loop so it is not applicable in all cases. If you have already a message loop in your second thread then it is fine. There are, I think, better solutions but it all depends of what you want to achieve. What I do in general is having a class ta wraps the threading functionality (have a member function that will be called from the thread) and then if I need to 'exchange' information, I'll do that through class members and I use events to notify the other thread and eventually critical sections to protect the access to the shared resources.
Cédric Moonen Software developer
Charting control [v1.2]Cedric Moonen wrote:
If you have already a message loop in your second thread then it is fine.
Yes ,one of the thread is a window thread, and the other is a worker thread, after the worker thread finishes it's working, it will Send(Post) a message with a large block of memory to the window thread. Thank you Cédric Moonen :)
-
Is it memory safe to use
SendMessage(PostMessage)
to pass parameters between threads? There are two threads A and B; // thread A want to send some message to thread B code sample about thread A:{ char sz[] = "Hello, CodeProject"; BOOL b = TRUE; SendMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, (LPRARM)b); //or PostMessage(hWnd, WM_SOMEMESSAGE, (WPARAM)sz, 0); }
Can I write code like this in thread B to retriecve the message?{ MSG msg GetMessage(&msg, hWnd, 0, 0); char* p = (char*)(msg.wParam) BOOL b = (BOOL)(msg.lParam) cout<<p<<endl; // can I get "Hello, CodeProject"? }
Thank you for your reply. :) -
not beautiful? :sigh: Is there some beautiful ways to send large objects between threads? How about Memory-mapped files? I think it is not beautiful too. Because after you copy some information in the memory-mapped file, you still need to tell the other thread to retrieve the information. Thank you! :)
Roughly speaking, usually processes need some technique to exchange data, while threads, by nature, already share data. What I mean is that you just don't need to pass messages to you thread but a bit refactoring to your design. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Roughly speaking, usually processes need some technique to exchange data, while threads, by nature, already share data. What I mean is that you just don't need to pass messages to you thread but a bit refactoring to your design. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]Thank you! I'll think twice before coding. :)
A Chinese VC++ programmer
-
Thank you! I'll think twice before coding. :)
A Chinese VC++ programmer
zengkun100 wrote:
A Chinese VC++ programmer
Greetings to China. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]