Closed lushengguo closed 1 month ago
Your example uses three threads (main, and two std::jthread instances), and all these threads somehow interact directly or indirectly with the AMQP::Connection instance. This is not good. This will result in undefined behavior as the order in which data is sent over the socket is out of control. Solution: do not use threads.
Here is a demo from linux platform with the newest code of AMQP-CPP;
I just simplified the code for a clear review, rabbitmq deployed on same linux machine, exchange/queue already declared.
In my code, I posted every opeartion of IO, creat channel/reliable and call method of it to boost::asio::io_context, and it's obvious that all amqp operation as it run in single thread;
This demo impls read and write, it could correctly handshake with rabbitmq and declare exchange/queue and bind them in one thread, also publish message in main thread, when I tried to publish message from different thread, error occurs, what confused me is that "LibBoostAsioHandler and AMQP::TcpConnection" works well in this testcase, so posting task from another thread to io_context is ok.
Readme.md said that I should impl IO by myself, I saw TcpConnection maintained some state of amqp connection, should I maintain those inner state either to ensure some IO sequence?
by the way, I'm using windows, so I have to do IO myself, just running test code on linux to make sure it's not platform's fault.