c++ - 线程 C++ 上的用户 I/O

标签 c++ multithreading boost nana

我这里有一些代码,其中包含一个窗口类(使用 Nana C++)和一些网络线程。但是,我似乎无法以任何方式输出给用户。我试过附加到文本框,使用消息框,打印到控制台,但它不会显示。这是 Nana 或 Boost.Thread 的问题吗?

如果 Boost.Thread 有问题,我可以切换到 std::thread,但我认为它不会起作用。

void thread()//Let's say this function is started on a thread and the window is started on main
{
    append(L"append test");
    MsgBox(L"msgbox test"):
}

最佳答案

有一个 Nana 演示说明了如何在其他线程中附加文本。

#include <nana/gui.hpp>
#include <nana/gui/widgets/textbox.hpp>
#include <nana/gui/widgets/button.hpp>
#include <mutex>
#include <condition_variable>
#include <thread>

int main()
{
    using namespace nana;

    form fm(API::make_center(300, 250));

    textbox txt(fm, rectangle(10, 10, 280, 190));
    button btn(fm, rectangle(10, 220, 200, 20));
    btn.caption("append texts in other thread");

    std::mutex mutex;
    std::condition_variable condvar;
    volatile bool running = true;

    std::thread thrd([&]
    {
        while(running)
        {
            std::unique_lock<std::mutex> lock(mutex);
            condvar.wait(lock);

            txt.append(L"append a new line\n", false);

            msgbox mb(L"hello");
            mb<<L"This is a demo";
            mb.show();
        }
    });

    btn.events().click([&]
    {
        std::lock_guard<std::mutex> lock(mutex);
        condvar.notify_one();
    });

    fm.events().unload([&]
    {
        running = false;
        std::lock_guard<std::mutex> lock(mutex);
        condvar.notify_one();   
    });

    fm.show();
    exec();
    thrd.join();
}

此演示是使用 Nana C++ Library 1.0.2 创建的,在 Windows/Linux 上运行良好

关于c++ - 线程 C++ 上的用户 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30310687/

相关文章:

c++ - 通过 boost::random 在 C++ 中随机生成 128 位(uint128_t)

c++ - 由于链接错误,我无法构建 boost_regex 代码

c++ - 在未排序的对数组中查找 K UNIQUE 最大元素

c++ - 获取 CUDA thrust::transform operator() 函数中的 vector 索引

java - Spring用单线程池并发处理多个队列

c# - 在这种情况下锁定是否保证多个修改在线程间是原子的?

c# - .NET Socketserver 最大并发连接数和积压的最大值

c++ - qi::parse boost 改变开始迭代器

c++ - 在开关中声明类对象,然后在开关外使用该变量

python - 如何找到给定斐波那契数的索引