用户i/o线程C

User I/O on threads C++

本文关键字:线程 用户      更新时间:2023-10-16

我这里有一些代码,该代码由窗口的类组成(使用NANA C )和一些用于网络的线程。但是,我似乎无法以任何方式向用户输出。我尝试使用消息框,打印到主机上的文本框,但不会出现。这是nana还是boost.thread?

的问题

如果这是boost的问题。线程我可以切换到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 linen", 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 库1.0.2创建此演示,在Windows/Linux上正常工作