
It definitely helps; it crashed 0 out of 10 times, which is enough for me (it crashed every time before, even without traffic). I didn't know asio objects weren't thread-safe.
It's explicitly stated in the docs: http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/serial_po... (Look at the page footer.) But in this particular case, the problem certainly was CancelIo issue, rather than thread-safety one.
I need to use them in a multithreaded environment; I have a reader and a writer thread. I suppose that wrapping every call to the serial_port object inside a post message would avoid any trouble but might be too slow. Am I right?
No, i don't think it would be slow, I believe the i/o itself would be *much* slower. But in any case, making "premature optimizations" is usually a bad idea. After you profile your application performance in a real-life scenario -- you'll be able to see where the bottlenecks are and to resolve them.
Are there some general guidelines I should follow when using asio objects in multithreaded environments?
You can find the most useful approaches in asio tutorial and examples: http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/tutorial.html http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html