简体   繁体   English

C++ 输入和 output 同时到控制台 window

[英]C++ Input and output to the console window at the same time

I'm writing a server(mainly for windows, but it would be cool if i could keep it multiplatform) and i just use a normal console window for it.我正在编写一个服务器(主要用于 windows,但如果我可以将它保持在多平台上会很酷),我只使用普通控制台 window 。 However, I want the server to be able to do commands like say text_to_say_here or kick playername, etc. How can i have a asynchronous input/output?但是,我希望服务器能够执行诸如 say text_to_say_here 或 kick playername 等命令。我怎样才能有异步输入/输出? I allready tried some stuff with the normal printf() and gets_s but that resulted in some really.... weird stuff.我已经用普通的 printf() 和 gets_s 尝试了一些东西,但这导致了一些非常......奇怪的东西。

I mean something like this 1我的意思是这样的1

thanks.谢谢。

Quick code to take advantage of C++11 features (ie cross-platform)利用 C++11 功能的快速代码(即跨平台)

#include <atomic>
#include <thread>
#include <iostream>

void ReadCin(std::atomic<bool>& run)
{
    std::string buffer;

    while (run.load())
    {
        std::cin >> buffer;
        if (buffer == "Quit")
        {
            run.store(false);
        }
    }
}

int main()
{
    std::atomic<bool> run(true);
    std::thread cinThread(ReadCin, std::ref(run));

    while (run.load())
    {
        // main loop
    }

    run.store(false);
    cinThread.join();

    return 0;
}

You can simulate asynchronous I/O using threads, but more importantly, you must share a mutex between the two read/write threads in order to avoid any issues with a thread stepping on another thread, and writing to the console on top of the output of another thread.您可以使用线程模拟异步 I/O,但更重要的是,您必须在两个读/写线程之间共享一个互斥锁,以避免线程踩到另一个线程并在 output 之上写入控制台时出现任何问题的另一个线程。 In other words std::cout , std::cin , fprintf() , etc. are not multi-thread safe, and as a result, you will get an unpredictable interleaving pattern between the two operations where a read or write takes place while another read or write was already happening.换句话说, std::coutstd::cinfprintf()等不是多线程安全的,因此,在发生读取或写入的两个操作之间会出现不可预知的交错模式。另一次读取或写入已经发生。 You could easily end up with a read trying to take place in the middle of a write, and furthermore, while you're typing an input on the console, another writing thread could start writing on the console, making a visual mess of what you're trying to type as input.您可能很容易在写入过程中尝试读取,此外,当您在控制台上输入输入时,另一个写入线程可能会开始在控制台上写入,从而使您的视觉混乱'正在尝试输入作为输入。

In order to properly manage your asynchronous read and write threads, it would be best to setup two classes, one for reading, and another for writing.为了正确管理异步读写线程,最好设置两个类,一个用于读取,另一个用于写入。 In each class, setup a message queue that will either store messages (most likely std::string ) for the main thread to retrieve in the case of the read thread, and for the main thread to push messages to in the case of the write thread.在每个 class 中,设置一个消息队列,该队列将存储消息(很可能是std::string )供主线程在读取线程的情况下检索,并在主线程的情况下将消息推送到写入线。 You may also want to make a special version of your read thread that can print a prompt, with a message pushed into its message queue by the main thread that will print a prompt before reading from stdin or std::cin .您可能还想制作一个可以打印提示的特殊版本的读取线程,主线程将消息推送到其消息队列中,主线程将在从stdinstd::cin读取之前打印提示。 Both classes will then share a common mutex or semaphore to prevent unpredictable interleaving of I/O.然后,两个类将共享一个公共互斥锁或信号量,以防止 I/O 的不可预测的交错。 By locking the common mutex before any iostream calls (an unlocking it afterwards), any unpredictable interleaving of I/O will be avoided.通过在任何iostream调用之前锁定公共互斥体(之后解锁),将避免任何不可预测的 I/O 交错。 Each thread will also add another mutex that is unique to each thread that can be used to maintain exclusivity over access to the class's internal message queue.每个线程还将添加另一个互斥锁,该互斥锁对每个线程都是唯一的,可用于维护对类内部消息队列的访问的排他性。 Finally, you can implement the message queues in each class as a std::queue<std::string> .最后,您可以将每个 class 中的消息队列实现为std::queue<std::string>

If you want to make your program as cross-platform as possible, I would suggest implementing this with either Boost::threads, or using the new C++0x std::threads libraries.如果您想让您的程序尽可能跨平台,我建议您使用 Boost::threads 或使用新的 C++0x std::threads 库来实现它。

If you ditch the console window and use TCP connections for command and control, your server will be much easier to keep multi-platform, and also simpler and more flexible.如果您放弃控制台 window 并使用 TCP 连接进行命令和控制,您的服务器将更容易保持多平台,并且更简单和更灵活。

You can try placing the input and output on separate threads.您可以尝试将输入和 output 放在不同的线程上。 I'm not quite sure why you want to do this, but threading should do the job.我不太确定你为什么要这样做,但线程应该可以完成这项工作。 :) :)

http://en.wikibooks.org/wiki/C++_Programming/Threading http://en.wikibooks.org/wiki/C++_Programming/Threading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM