As others have already said, how you wrote it is fine, and in the general case, nothing will happen right away when you pass a pointer to an object in the stack on another thread until everything is synchronized. However, I tend to compress a bit in this case, because things that seem thread safe can go out of their intended order when an exception occurs, or if one of the threads is associated with asynchronous I / O callbacks. In the case of an exception in another thread during your SendMessage call, it can immediately return 0. If the exception is later handled in another thread, you may have an access violation. Another potential danger is that everything stored on the stack can never be forcibly removed from another thread. If it is stuck waiting for some callback, object, etc. Forever, and the user decided to cancel or exit the application, there is no way for the workflow to be sure that the stalled thread has cleared all objects in its stack.
My point is this: in simple scenarios, as you described, where everything works fine, nothing changes, and no external dependencies work, sharing pointers on the local stack is safe, but since allocating to a bunch is really the same as simple, and this gives you the ability to explicitly control the lifetime of an object from any stream in extenuating circumstances, why not just use a bunch?
Finally, I highly recommend that you be very careful with the void * chunk_ member of your MyData structure, as it is not thread safe, as described if it is copied to another stream.
source share