Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A question about SendMessage and PostMessage

A question about SendMessage and PostMessage

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zengkun100
    wrote on last edited by
    #1

    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. :)

    C K 2 Replies Last reply
    0
    • Z zengkun100

      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. :)

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #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]

      Z 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        Z Offline
        Z Offline
        zengkun100
        wrote on last edited by
        #3

        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 use new operator to allocate memory on the heap, and after GetMessage returned use delete to free memory?

        C 1 Reply Last reply
        0
        • Z zengkun100

          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 use new operator to allocate memory on the heap, and after GetMessage returned use delete to free memory?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Yes, in that case it is safe (even if it is not beautifull ;P ).

          Cédric Moonen Software developer
          Charting control [v1.2]

          Z 1 Reply Last reply
          0
          • C Cedric Moonen

            Yes, in that case it is safe (even if it is not beautifull ;P ).

            Cédric Moonen Software developer
            Charting control [v1.2]

            Z Offline
            Z Offline
            zengkun100
            wrote on last edited by
            #5

            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! :)

            C C 2 Replies Last reply
            0
            • Z zengkun100

              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! :)

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              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]

              Z 1 Reply Last reply
              0
              • C Cedric Moonen

                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]

                Z Offline
                Z Offline
                zengkun100
                wrote on last edited by
                #7

                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 :)

                1 Reply Last reply
                0
                • Z zengkun100

                  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. :)

                  K Offline
                  K Offline
                  KarstenK
                  wrote on last edited by
                  #8

                  This isnt safe, a better word for it is dangerous. You better work with integer/enum values to singalize the state (for instance String IDs) or a global object which stores the information.

                  Greetings from Germany

                  1 Reply Last reply
                  0
                  • Z zengkun100

                    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! :)

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    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]

                    Z 1 Reply Last reply
                    0
                    • C CPallini

                      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]

                      Z Offline
                      Z Offline
                      zengkun100
                      wrote on last edited by
                      #10

                      Thank you! I'll think twice before coding. :)

                      A Chinese VC++ programmer

                      C 1 Reply Last reply
                      0
                      • Z zengkun100

                        Thank you! I'll think twice before coding. :)

                        A Chinese VC++ programmer

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        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]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups