You can simulate asynchronous I / O using threads, but more importantly, you should share the mutex between the two read / write streams in order to avoid problems with the stream advancing to another stream, and write to the console on top of the output of the other stream. In other words, std::cout , std::cin , fprintf() , etc. They are not multithreaded, and as a result you will get an unpredictable pattern of alternation between two operations in which reading or writing occurs, while another reading or writing was already there. You could easily finish reading by trying to take place in the middle of the recording, and in addition, while you type in the input to the console, another stream of letters can start writing to the console, creating a visual mess of what you're trying to enter as input.
To properly manage asynchronous read and write streams, it would be better to configure two classes: one for reading and the other for writing. In each class, set up a message queue that either saves messages (most likely std::string ) for the main stream, which will be retrieved in case of a read stream, and for the main stream for entering messages in the case of a write stream, You may also want to create A special version of the read stream that can print the invitation, with the message placed in the message queue by the main stream, which will print the invitation before reading from stdin or std::cin . Then both classes will have a common mutex or semaphore to prevent unpredictable I / O interleaving. By blocking the shared mutex before any iostream calls (after it is unlocked), any unpredictable I / O rotation can be avoided. Each thread will also add another mutex, which is unique to each thread, which can be used to provide exclusivity when accessing the classβs internal message queue. Finally, you can implement message queues in each class as std::queue<std::string> .
If you want to make your program as cross-platform as possible, I would suggest implementing it either with Boost :: threads or with the new std :: threads C ++ 0x libraries.
Jason source share