有主线程等待升压线程完成一个任务(但不能完成) [英] Have main thread wait for a boost thread complete a task (but not finish)

查看:123
本文介绍了有主线程等待升压线程完成一个任务(但不能完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在做一个线程等待另一个完成之前继续执行很多,但是这不是我想做的事。我不是很熟悉使用任何多线程API,但现在我努力学习的推动作用。我的情况是,我用我的主线程(起始一名来自INT主())来创建一个类,负责与主界面交互的一个实例。那么A类函数被调用创建一个线程推动这反过来又创造了GUI和运行的消息泵。我想要做的事情是,当我的主线程调用的类成员函数来创建GUI,我不希望函数返回,直到我告诉它从新创建的线程。这样,我的主线程不能继续,并从该直到线程已经完成GUI创建并进入消息循环与GUI线程交互的GUI类调用更多的功能。我想,我也许能够弄清楚,如果它是多个升压线程对象相互交融,但是当它是主线程(非升压对象)和一个升压线程对象的互动,我迷路。最后,我想在我的主线程调用类功能(以及其他任务)的循环,以检查是否为输入的任何新的输入到GUI用户(购买的消息循环检测到任何变化更新成一个结构和改变布尔告诉类中的功能发生了改变主线程)。任何这样的建议将是极大的AP preciated。

I have found plenty on making one thread wait for another to finish executing before continuing, but that is not what I wanted to do. I am not very familiar with using any multi-threading apis but right now I'm trying to learn boost. My situation is that I am using my main thread (the starting one from int main()) to create an instance of a class that is in charge of interacting with the main GUI. A class function is then called that creates a boost thread which in turn creates the GUI and runs the message pump. The thing I want to do is when my main thread calls the classes member function to create the GUI, I don't want that function to return until I tell it to from the newly created thread. This way my main thread can't continue and call more functions from the GUI class that interact with the GUI thread until that thread has completed GUI creation and entered the message loop. I think I may be able to figure it out if it was multiple boost thread objects interacting with each other, but when it is the main thread (non-boost object) interacting with a boost thread object, I get lost. Eventually I want a loop in my main thread to call a class function (among other tasks) to check if the user as entered any new input into the GUI (buy any changes detected by the message loop being updated into a struct and changing a bool to tell the main thread in the class function a change has occurred). Any suggestions for any of this would be greatly appreciated.

这是由主线程调用成员函数。

This is the member function called by the main thread.


int ANNGUI::CreateGUI()
{
    GUIMain = new Main();
    GUIThread = new boost::thread(boost::bind(&Main::MainThreadFunc, GUIMain));
    return 0;
};

这是升压线程启动功能。

This is the boost thread starting function.


void Main::MainThreadFunc()
{
    ANNVariables = new GUIVariables;
    WndProc = new WindowProcedure;
    ANNWindowsClass = new WindowsClass(ANNVariables, WndProc);
    ANNWindow = new MainWindow(ANNVariables);
    GUIMessagePump = new MessagePump;
    ANNWindow->ShowWindows();
    while(true)
    {
        GUIMessagePump->ProcessMessage();
    }
};

顺便说一句,一切都编译正常,当我运行它,它的工作原理我只是把睡眠()在主线程,所以我可以使用GUI打一点。

BTW, everything compiles fine and when I run it, it works I just put a sleep() in the main thread so I can play with the GUI a little.

推荐答案

如果你想等待线程来完成简单的使用join()方法(http://www.boost.org/doc/libs/1_45_0/doc/html/thread/thread_management.html#thread.thread_management.thread.join)

If you want to wait for the thread to finish simply use join() (http://www.boost.org/doc/libs/1_45_0/doc/html/thread/thread_management.html#thread.thread_management.thread.join)

如果你正在等待一个信号,一个简单的解决方法是使用一个互斥。提升的障碍互斥应该为你工作:

If you are waiting for a signal a simple solution is to use a mutex. The barrier mutex of boost should work for you:

<一个href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/thread/synchronization.html#thread.synchronization.barriers\">http://www.boost.org/doc/libs/1_45_0/doc/html/thread/synchronization.html#thread.synchronization.barriers

您可以从提高升压线程或非线程提振互斥。

You can boost mutex from boost threads or non boost threads.

这篇关于有主线程等待升压线程完成一个任务(但不能完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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