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

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

问题描述

我发现很多让一个线程等待另一个线程完成执行,然后继续,但这不是我想做的。我不是很熟悉使用任何多线程apis,但现在我试图学习boost。我的情况是,我使用我的主线程(从int main()开始一个)创建一个类的实例,负责与主GUI交互。然后调用一个类函数来创建一个boost线程,该线程又创建GUI并运行消息泵。我想做的事情是当我的主线程调用类成员函数创建GUI,我不想让该函数返回,直到我告诉它从新创建的线程。这样,我的主线程不能继续,并从GUI类中调用更多的函数与GUI线程交互,直到该线程完成GUI创建并进入消息循环。我想我可以能够计算出来,如果它是多个提升线程对象相互交互,但当它是主线程(非提升对象)与提升线程对象交互,我迷路了。最终我想在我的主线程中调用一个类函数(以及其他任务)来检查用户是否输入任何新的输入到GUI(购买任何更改检测到的消息循环正在更新到结构和更改bool告诉主线程在类函数中发生了变化)。



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

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

这是boost线程启动函数。

 
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();
}
};


BTW,一切都编译得很好,当我运行它,它的工作原理我只是在主线程sleep()

如果你想等待线程完成,只需使用join()(http:/ / /www.boost.org/doc/libs/1_45_0/doc/html/thread/thread_management.html#thread.thread_management.thread.join)



如果您正在等待对于信号一个简单的解决方案是使用互斥。 boost的屏障互斥对你应该有效:



http://www.boost.org/doc/libs/1_45_0/doc/html/thread/synchronization.html#thread.synchronization.barriers p>

您可以从boost线程或非boost线程中提升互斥量。


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();
    }
};

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.

解决方案

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:

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.

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

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