I read an article on the multi-threaded software project http://drdobbs.com/architecture-and-design/215900465 , it says that it’s best practice to “replace shared data with asynchronous messages. As much as possible, prefer to keep separate stream data ( not separated) and allow threads to instead communicate via asynchronous messages that transmit copies of the data. "
What bothers me is that I don’t see the difference between using shared data and message queues. Now I'm working on a non-GUI project on Windows, so let me use Windows message queues. and take the traditional problem of consumer producers as an example.
Using common data, there will be a common container and a lock protecting the container between the producer stream and the consumer stream. When the product is releasing a product, first wait for the lock, then write something into the container, then release the lock.
Using a message queue, a producer can simply PostThreadMessage without a block. and this is the advantage of an asynchronous message. but I think there is some kind of lock that protects the message queue between two threads, otherwise the data will definitely be corrupted. calling PostThreadMessage just hides the details. I don’t know if my guess is right, but if it’s true, the advantage seems to no longer exist, since both methods perform the same thing, and the only difference is that the system hides details when using message queues.
ps. perhaps the message queue uses a non-blocking connector, but I could use the parallel container as before. I want to know how the message queue is implemented, and is there a performance difference between the two methods?
update: I still don't understand the concept of asynchronous message if message queue operations are still blocked elsewhere. Correct me if my assumption is wrong: when we use shared containers and locks, we will block in our stream. but when using message queues, the thread itself returned immediately and left the lock operation on some system thread.
source share