C ++输入和输出到控制台窗口的同时 [英] C++ Input and output to the console window at the same time

查看:531
本文介绍了C ++输入和输出到控制台窗口的同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的服务器(主要为窗口,但它会很酷,如果我能保持多平台),我只是使用普通的控制台窗口吧。不过,我希望服务器能够做到像说text_to_say_here或踢playername,等我如何能有一个异步输入/输出命令?我试着媒体链接一些东西与正常printf()和gets_s但导致一些真正....怪异的东西。

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. 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? I allready tried some stuff with the normal printf() and gets_s but that resulted in some really.... weird stuff.

我的意思是这样的 1

感谢。

推荐答案

快速code,以充分利用C ++ 11的特性(即跨平台)

Quick code to take advantage of C++11 features (i.e. cross-platform)

#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;
}

这篇关于C ++输入和输出到控制台窗口的同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆